Passing a clearer description of the RSpec `its` method

I am RSpec newb, but I really like how easy it is to write tests, and I am constantly reorganizing them so that they are cleaner when I learn new RSpec functions. So, initially I had the following:

describe Account do context "when new" do let(:account) { Account.new } subject { account } it "should have account attributes" do subject.account_attributes.should_not be_nil end end end 

Then I found out about its method, so I tried to rewrite it as such:

 describe Account do context "when new" do let(:account) { Account.new } subject { account } its(:account_attributes, "should not be nil") do should_not be_nil end end end 

This fails due to its not taking 2 arguments, but deleting the message works fine. The problem is that if the test fails, the message in the "Failure with Examples" section just says

 rspec ./spec/models/account_spec.rb:23 # Account when new account_attributes 

which is not too useful.

So, is there a way to pass the message to its , or is it even better that it automatically outputs a sound message?

+4
source share
3 answers

You can define a custom RSpec resolver:

 RSpec::Matchers.define :have_account_attributes do match do |actual| actual.account_attributes.should_not be_nil end failure_message_for_should do "expected account_attributes to be present, got nil" end end describe Account do it { should have_account_attributes } end 
+3
source

You can also write: its(:account_attributes) { should_not be_nil }

See https://www.relishapp.com/rspec/rspec-core/v/2-14/docs/subject/attribute-of-subject

Note that โ€œitโ€ will be extracted from rspec-core into the gem with the release of rspec 3.

+1
source

It seems that a relatively simple monkey patch will allow you to search for what you are looking for.

Look at the source of your version of gspec-core gem. I am on 2.10.1. In the file lib/rspec/core/subject.rb I see its method.

Here is my revised version - I changed the def line and the line after that.

Caution - this most likely depends on the version! Copy the method from your version and change it the same way I do. Please note that if rspec-core developers are doing a major restructuring of the code, the patch may need to be completely different.

 module RSpec module Core module Subject module ExampleGroupMethods # accept an optional description to append def its(attribute, desc=nil, &block) describe(desc ? attribute.inspect + " #{desc}" : attribute) do example do self.class.class_eval do define_method(:subject) do if defined?(@_subject) @_subject else @_subject = Array === attribute ? super()[*attribute] : _nested_attribute(super(), attribute) end end end instance_eval(&block) end end end end end end end 

This patch can probably be placed in your spec_helper.rb .

Now use:

 its("foo", "is not nil") do should_not be_nil end 

Error output:

 rspec ./attrib_example_spec.rb:10 # attr example "foo" is not nil 

If you omit the second arg argument, the behavior will be the same as the failed method.

0
source

Source: https://habr.com/ru/post/1434229/


All Articles