[rspec-users] A bit confused with the use of assign() to test views
ct9a
anexiole at gmail.com
Wed Aug 3 04:24:18 EDT 2011
hi guys,
I'm trying to pick up Rspec to port an existing application in rails
2.3.8 to rails 3.
I'm using the pragmatic "The Rspec book" (dec 2010) as a reference.
read around the book and the rspec docs.
1) assign method
- syntax: assign( <symbol name>, <block usually calling double()
or mock_model)>
- what it means to me: run the codes in the block and assign the
value evaluated to the symbol name.
2) let method
syntax: let(method_name>) { … }
- will not run until it is called
-used with before(), end() to set up usually instance variables
-the contents in its block will be evaluated when called
-here's what http://rdoc.info/gems/rspec/1.3.2/frames says when i
looked up the source for let.
------ Documentation extract for "let" (start) ----------------------
def let(name, &block)
define_method name do
@assignments ||= {}
@assignments[name] ||= instance_eval(&block)
end
end
------ Documentation extract for "let" (end) ----------------------
-to call the contents of the block, we will refer to it by the method
name (which is a symbol)
There's this example below (from the rspec book page 338) which I am
a little confused with.
------------ Extract start ----------------------------
1 require 'spec_helper'
2
3 describe "messages/new.html.erb" do
4 let(:message) do
5 mock_model("Message").as_new_record.as_null_object
6 end
7
8 before do
9 assign(:message, message)
10 end
11
12 it "renders a form to create a message" do
13 render
14 rendered.should have_selector("form",
15 :method => "post",
16 :action => messages_path
17 ) do |form|
18 form.should have_selector("input", :type => "submit")
19 end
20 end
21
22 it "renders a text field for the message title" do
23 message.stub(:title => "the title")
24 render
25 rendered.should have_selector("form") do |form|
26 form.should have_selector("input",
27 :type => "text",
28 :name => "message[title]",
29 :value => "the title"
30 )
31 end
32 end
33 end
------------ Extract end ----------------------------
My question is, line 9 seems to imply that the content of :message
(the mocked Message object) is being
assigned to the 'message' object but as per what I have read in the
Rspec book, the first argument in the call to assign() is actually the
variable that is getting assigned with.
Yet, the extract above still works when i run "rake spec".
I'm a bit confused.
Please help.
Thanks
More information about the rspec-users
mailing list