Reputation: 4523
I'm reading the Ruby on Rails Tutorial", and on a page regarding to Rspec tests, there is this line:
subject {page}
We can omit
the word from, for example:
it { page.should have_content('Help') }
What is subject
? What is page
?
Also, why is it declared subject{}
and it{}
? I saw that the braces ("{}"), are used when you are declaring a hash like:
user = {name: 'Jhon', lastName: 'Smith'}
Upvotes: 1
Views: 1754
Reputation: 486
I hope this code can make it more clear:
Say you have a class:
class A
def initialize(m, n)
@m = m
@n = n
end
def m
@m
end
def n
@n
end
end
Than to test that method m
returns the correct value you can use this code with subject:
subject { A.new("lorem", "ipsum") }
specify { subject.m.should == "lorem" }
specify { subject.n.should == "ipsum" }
In the example above subject
will be the object that you created in subject { A.new("lorem", "ipsum") }
.
Or you can use code like this to make the rspec examples shorter:
subject { A.new("lorem", "ipsum") }
its(:n) { should == "lorem" }
its(:m) { should == "ipsum" }
And the page
in your post was just an object which was created somewhere else in the code of the test.
Upvotes: 2
Reputation: 22859
subject
is an rspec method that takes a block which will be the implicit topic of your assertions. page
refers to (probably) Capybara's page
object, which is available in rspec tests using Capybara where you have done something like visit('/some/url')
.
Upvotes: 4