Today I Learned

9 posts about #ruby

FactoryBot: Constructing objects using Dry::Types

If you face error similar to the one below

Dry::Struct::Error: [YourClassName.new] :some_attribute_name is missing in Hash input

when building objects of the class using Dry::Types and FactoryBot, be advised, that

Although factory_bot is written to work with ActiveRecord out of the box, it can also work with any Ruby class. For maximum compatibility with ActiveRecord, the default initializer builds all instances by calling new on your build class without any arguments. It then calls attribute writer methods to assign all the attribute values. While that works fine for ActiveRecord, it actually doesn’t work for almost any other Ruby class.

The fix is to add following line to your factory definition

initialize_with  { new(attributes) }

How to change stubbed return value with another stub?

Simple - just re-define spy as a result of another stub

valid_token = instance_double(ValidToken)
allow(ValidToken).to receive(:new) { valid_token }
allow(valid_token).to receive(:to_s) { '123' }
allow(valid_token).to receive(:clear!) do
   allow(valid_token).to receive(:to_s) { '456' }
end
valid_token = ValidToken.new
valid_token.to_s # 123
valid_token.clear!
valid_token.to_s # 456

Stubbing responses from AWS services

We have started integration with Amazon SQS recently and did need to write some unit tests related to it. Unfortunately stubbing AWS client library the regular way turned out to be pretty cumbersome and challenging. Fortunately AWS SDK for ruby provides tools that make it pretty comfortable.

# Simple stubbing...
sqs_response_mock = Aws::SQS::Types::ReceiveMessageResult.new
sqs_response_mock.messages << Aws::SQS::Types::Message.new(body: 'abc')
Aws.config[:sqs] = {
    stub_responses: {
        receive_message: sqs_response_mock
    }
}

# ...allows properly polling the queue
poller = Aws::SQS::QueuePoller.new('https://sqs.us-east-2.amazonaws.com/123/some-queue')
poller.poll do |msg|
  puts msg.body
end

# => abc

Documentation can be found here

Passing multiple exceptions to rescue

To rescue multiple classes of exceptions, you have to pass them as a list to rescue method. Passing as an array won’t work, unless you prefix it with * (effectively breaking it into a list)

# Works
begin
  # some code here    
rescue ExceptionA, ExceptionB
  puts "uff, thanks!"
end

# Doesn't work
begin
  # some code here    
rescue [ExceptionA, ExceptionB]
  puts "uff, thanks!"
end

# Works
EXCEPTIONS = [ExceptionA, ExceptionB]
begin
  # some code here    
rescue *EXCEPTIONS
  puts "uff, thanks!"
end

Selenium::WebDriver::Error::NoSuchDriverError

We’ve recently experienced some peculiar errors when processing capybara-based automation scripts on Heroku. Most of the time, the error returned did not show anything useful…

Selenium::WebDriver::Error::NoSuchDriverError: no such session

yet for a brief period of time, following error was reported when attempting to access capybara session

Selenium::WebDriver::Error::UnknownError: unknown error: session deleted because of page crash
from tab crashed

Finally, after spotting this comment we’ve reduced chrome window size from 1920,1200 to 1440,900 and the problem is no longer present.

The root reason is unknown, but most likely it is at least partially related to running out of memory (reference). Most of recommendations when using docker in this scenario, was to increase shm-size, by providing --shm-size=2g to docker run. That was not an option for us though…

Hope it helps in case you run into similar situation.

Difference between <<~ and <<-

If you use heredoc in ruby you can choose one of two syntaxes the first <<- it takes all whitespace before stared of text so

def custom_html  
  <<-HTML
    <html>
      <body>
      </body>
    </html>
  HTML
end    

so if you will get string with all withspaces " <html>\n <body>\n </body>\n </html>\n" second <<~ will strip it so

def custom_html  
  <<~HTML
    <html>
      <body>
      </body>
    </html>
  HTML
end

will give you will get text "<html>\n <body>\n </body>\n</html>\n"

Capybara::Selenium

When you not only use Capybara for rspec, you would like to change the wait time to visit the page, so I think it can be useful.

   Capybara.register_driver :selenium do |app|
      client = Selenium::WebDriver::Remote::Http::Default.new
      client.read_timeout = 20
      Capybara::Selenium::Driver.new(app, browser: :chrome,  http_client: client)
    end