In RSpec, some matchers can take another matcher as an argument. It is sometimes useful for writing an expectation for a collection. Let’s say we want to test that an array contains a string that includes a particular text. We can do that by combining two matchers: include and match as follows.
expect(an_array).to include(match(/Lorem/)) # Of course we also could do: # expect(an_array.grep(/Lorem/)).not_to be_empty # or # expect(an_array).to be_any {|item| /Lorem/ === item } I think that in this case, the expectation with include and match is much more concise than the commented-out alternative ways above.
Read more →
Sometimes I want to create a tiny one-off script that intended to run on a sandbox environment like a Docker container and needs only one file. Most of the time, I also need test frameworks to do some refactoring. It can be done with bundler/inline and rspec/autorun.
require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'rspec' end def doit 'return a string' end require 'rspec/autorun' RSpec.describe '#doit' do example do expect(doit).to be_a(String) end end Usually, Bundler reads the dependencies from Gemfile. However, we can forgo this file by using gemfile block instead. As running the script, Bundler will install gems that are required by the block but not installed in the system. So the initial run may take a little longer time for installation. Note that you cannot place require 'rspec/autorun' before the gemfile block since some of the dependencies might not be installed yet before that.
Read more →
Today I learned that RSpec’s #to method takes the 2nd argument as its custom failing-message.
expect(actual_value).to eq(expected_value), message Let’s say we are going to test a JSON object. The following example expects a part of the JSON object response_json['eye_colour'] to be "blue".
require 'net/https' require 'uri' require 'json' RSpec.describe do example do response = Net::HTTP.get_response(URI('https://swapi.dev/api/people/1/')) response_json = JSON.parse(response.body) expect(response_json['eye_colour']).to eq('blue') end end Unfortunately, this test fails and displays messages like this:
Failures: 1) is expected to eq "blue" Failure/Error: expect(response_json['eye_colour']).to eq('blue') expected: "blue" got: nil (compared using ==) From this output, we could guess either response_json['eye_colour'] is set to nil or the key eye_colour is not defined. However, there is no more information here. To find out the cause of the error, an additional print-debug would be needed.
Read more →