This should have an easy answer, but I'm struggling to find it (checked the RSpec documentation, EverydayRails Testing with RSpec, Google results). In my model specifications, I would like to include basic attribute specifications as follows:
describe Foo do describe "basic attributes" do before { @foo = create(:foo) } subject { @foo } it { should be_valid } it { should respond_to(:color) } it { should respond_to(:height) } it { should respond_to(:some_other_attribute) } it { should respond_to(:you_get_the_idea) } ...
I like these specifications because if there is some kind of error in my factory model and / or some kind of error, these specifications help me quickly identify it.
I have included expect syntax in all other specifications, and I like the way it reads, but how to use it here? One option might be
expect(@foo).to respond_to(:color)
And maybe still
expect(it).to respond_to(:color)
The former involves duplication, which should be avoided by the syntax, but the latter seems strange to me (it can only be me).
I understand that this issue is more about style than functionality *, but we, the Ruby developers, are honest about the style, and I want to adhere to standard practices and have readable, idiomatic code. Any help is appreciated. Thank.
UPDATE: By the way, none of my proposed options work. They both throw undefined method 'expect' errors. Now I'm really confused!
Thinking about the error, I understand this because the should specifications above are within the same line block. Then there is confusion, how can I write a one-line block with wait syntax? In the light of this update, the issue of functionality is very important, and I will be happy to hear the thoughts of others.
4/2015 UPDATE
rspec > 3.0 added another way to handle them, and it looks like rspec ~> 4.0 away with the should syntax. Per Miron Masters:
Some users have expressed confusion as to how this should relate to wait syntax, and if you can continue to use it. It will continue to be available in RSpec 3 (again, regardless of syntax configuration), but we also added an alternative API that is a bit consistent with the wait syntax:
describe Post do it { is_expected.to allow_mass_assignment_of(:title) } end
is_expected is defined very simply as an expectation (subject), and also supports negative expectations through an is_expected.not_to matcher. [...]
In RSpec 3, we have kept the if syntax, and it is available by default, but you will get a deprecation warning if you use it without explicitly enabling it. This will pave the way for disabling it by default (or potentially extracted into a separate stone) in RSpec 4, while minimizing the confusion for beginners arriving at RSpec through an old tutorial.