开发者

Rspec: testing assignment of instance variable

开发者 https://www.devze.com 2023-02-05 18:30 出处:网络
Using Rspec with Factory Girl. Tryi开发者_如何学编程ng to check out what data is being assigned in my controller (and test against it).Every post I\'ve read says I should be able to get something out

Using Rspec with Factory Girl. Tryi开发者_如何学编程ng to check out what data is being assigned in my controller (and test against it). Every post I've read says I should be able to get something out of assigns() but it keeps returning nill

Controller

def index
 @stickies = Sticky.where(:user_id => current_user.id)
end

Spec

it "should assign stickies" do
  foo = assigns(:stickies)
  puts "foo = #{foo}"
end

Output

foo = 

Am I using the wrong syntax? Is there a better way to do this? Thanks!!


You have to invoke the action first


describe StickiesController do
  describe "GET index" do
    it "should assign stickies" do
      get :index
      assigns(:stickies).should_not be_nil
    end
  end
end


If you are using the rspec > 2.99 you can use:

expect(assigns(:stickies)).not_to be_nil

0

精彩评论

暂无评论...
验证码 换一张
取 消