I use Rails 3 and RSpec 2.6.0.
Not sure if that's possible, but here is what I would like to do:
describe UsersController do
let(:user) { Fabricate :user }
describe "GET /user/:id" do
should_return_401_code_if_user_is_not_confirmed :get, :show, :id => user.id
end
describe "PUT /user/:id" do
should_return_401_code_if_user_is_not_confirmed :put, :update, :id => user.id
end
end
I tried to implement the macro like this:
module ControllerMacros
def should_return_401_code_if_user_is_not_confirmed(verb, action, params = {})
it "returns a 401 code if the user is not an admin" do
send verb, action, params
response.code.should == "401"
end
end
end
But when running those specs I get the error undefined local variable or method 'user'
. I tried to switch to a @user variable defined in a before block, but it does not work either. I suspect it's because I'm not in a example block.
Is it possible to pass to a controller macro arguments defined by let or in a before block?
Thanks!
From reading through this thread on the rspec-users listserve, it appears the answer is no. The issue is that your let/before vars aren't being initialized until the it
block in the body of your macro, so you can't reference them as parameters to the macro call.
I'm trying to do essentially the same thing in a site I'm working on. The solution I'm going with is to call the macro method with a block that will return the params hash you want, and then use instance_eval
in the macro body to evaluate the block using the macro's scope.
#spec
...
describe "GET /user/:id" do
should_return_401_code_if_user_is_not_confirmed :get, :show { {:id => user.id} }
end
#macro
...
def should_return_401_code_if_user_is_not_confirmed(verb, action, ¶ms_block)
it "returns a 401 code if the user is not an admin" do
params = instance_eval ¶ms_block
send verb, action, params
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With