@request object not available in functional test when using nested contexts and test helper method to make request

3 views
Skip to first unread message

infra guy

unread,
Nov 5, 2009, 5:05:41 PM11/5/09
to shoulda
Hi folks,

I'm having troubling getting access to the @request object for shoulda
tests within a context.

As a part of my setup block, I call a test helper method which
actually makes the request. Within this helper method, I have access
to the @request object. The helper method returns the @request object
to the caller. I assign this to a var in my setup block. I can
successfully access the @request object data after this call is made
and the var is assigned (within the setup block). Once out of the
setup block, I can no longer access this var (and therefore the
@request data).

From a non-shoulda/context (i.e. normal rails test), I can access the
@request data returned from my test helper.

Does anyone have any ideas? I'm thinking that this may be a setup
block scoping behavior of shoulda that I'm not aware of.

Here's an extraction of the relevant parts of my code:
http://pastie.org/685576

Thanks for any help.

François Beausoleil

unread,
Nov 11, 2009, 3:24:46 PM11/11/09
to sho...@googlegroups.com
Hi,

Like many, you have confused the class vs instance scopes.

class CommentsControllerTest < ActionController::TestCase
# in class scope
context "x" do
setup do
# in instance scope
# @response and friends are available
end

# back in class scope
end
end

In your case, you want to wrap the logger call at lines 22-23 in a
should:

class CommentsControllerTest < ActionController::TestCase
context "successful POST attempt" do
setup do
my_response = self.successful_post("xml")
Rails.logger "my_response: #{my_response.body}"
end

should "report the response" do
Rails.logger "@response: #{@response.body}"
end
end
end

Hope that helps.
--
François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/

Reply all
Reply to author
Forward
0 new messages