I feel like I'm missing something pretty basic, but I can't seem to figure it out, either.
When doing the following spec:
require 'spec_helper'
describe "/mymodel/show.html.erb" do
before(:each) do
@mymodel = Factory(:mymodel)
@user = Factory(:user)
assign(:web_tip, @mymodel)
assign(:current_user, @user)
end
it "renders attributes in <p>" do
render
rendered.should have_text(/somevalue/)
end
end
I get an error that the local variable current_user is undefined (the view's layout wants to call it to show the current login status).
I'm on Rails 3, Rspec 2.6.4, and thought I was following the current docs correctly.
This one is old, but for the sake of Google:
assign
only creates instance variables. However, current_user
is a helper method (or a local variable). Assuming you're not entirely relying on view testing, you can use the following before
block to make it work:
before(:each) do
view.stub(:current_user) { User.new }
end
what worked for me (with a similar problem) was something like:
@controller.stub(:current_user) { @user }
I think the problem is that current user is not an assignation (instance variable), is a value in the session
.
Hope it helps
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