I ran script/generate rspec_scaffold page title:string body:text and
have been reading on the generated code in my spare time in an attempt
to fully understand rspec. It mostly makes a lot of sense except for
the square brackets in the snippet below. To my best understanding,
[mock_page] would be an array containing mock_page. That almost makes
sense, buy why wouldn’t the variable mock_page simply be an array like @pages. You wouldn’t say [@pages].
I can’t get my head wrapped around the syntax.
Thanks in advance.
pages_controller_spec.rb
def mock_page(stubs={}) @mock_page ||= mock_model(Page, stubs)
end
describe “responding to GET index” do
it "should expose all pages as @pages" do
Page.should_receive(:find).with(:all).and_return([mock_page])
get :index
assigns[:pages].should == [mock_page]
end
To my best understanding,
[mock_page] would be an array containing mock_page. That almost makes
sense, buy why wouldn’t the variable mock_page simply be an array like @pages.
What you say is completely correct. In the code you give, mock_page
is defined as singular. It represents a single mock instance of a
Page model. It isn’t an array because it isn’t defined that way. But
you can (and do) put it in an array to represent a collection of
instances of the Page model.
You wouldn’t say [@pages].
No, you usually wouldn’t (though you could). But if you had a @page
variable that represented just one Page, it would be quite sensible
and common to say @pages == [@page].
That’s really all that mock_page is doing.
–
Have Fun,
Steve E.
Deep Salt Team
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.