its
isn't core to RSpec. One the of the focuses of RSpec is on the documentation aspects of tests. Unfortunately, its
often leads to documentation output that is essentially lies. Consider this spec:
User = Struct.new(:name, :email)
describe User do
subject { User.new("bob") }
its(:name) { should == "bob" }
end
$ bin/rspec user_spec.rb --format doc
User
name
should == "bob"
Finished in 0.00085 seconds
1 example, 0 failures
Randomized with seed 25153
It's not a true behavior of User#name
that it always equals "bob". its
generally leads to output that is true in a specific case, but false in the general case, and doesn't help understanding when reading the documentation output.
Those who like its
tend to really like it and want to continue to evolve it to do more and more powerful and terse things, such as having it support arguments. We're uncomfortable with making its
more and more meta (it's already meta enough!). We were planning on moving its
into an external gem when we removed it from rspec-core, and @dnagir has already done that:
Recently, we've also found some cases where there was some surprising, non-intuitive behavior with example groups that use subject
and its
:
rspec/rspec-core#768 (comment)
I think its
gains you terseness at the expense of clarity, and in my judgement, it's a poor tradeoff.
I also feel like rspec-given is a better direction to go with one-liners. If you like one-liners, rspec-given encourages Given/When/Then one-liners, with no example docstrings at all. It's more of a unified vision for this kind of thing than the its
method in rspec-core.
Finally, you can read @dchelimsky's thoughts on its
from a while ago.
@myronmarston I don't think that's a problem of
its
. Actually we need to talk not about successful output, which is rarely investigated, but about failed one.it { expect(subject.name).to eq "Bob" }
- if subject doesn't return 'Bob', then output will notify us that the name should be equal to 'Bob'. The problem still exists even withoutits
.To get well documented output that make developer free to not open spec file we need to write the next:
it { expect(User.new('bob', 'bobs@mail.com').name)to eq 'bob' }
so the output completely describe what we need to do to fix a bug. I think it's good if you have a simple enough context, that you can put into expectation. But in the most of the cases wee need to describe context once at the beginning, and test against it many times.
I believe, if we start to describe "true behavior" of User#name, we will end up with a complicated description of
it