Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec 2 use of assign for view specs

Tags:

rspec2

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.

like image 760
Paul Avatar asked Aug 11 '11 05:08

Paul


2 Answers

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
like image 52
Carsten Avatar answered Nov 09 '22 01:11

Carsten


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

like image 32
eloyesp Avatar answered Nov 09 '22 02:11

eloyesp