Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

ruby on rails - Rspec: testing assignment of instance variable

Using Rspec with Factory Girl. Trying 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!!

question from:https://stackoverflow.com/questions/4720135/rspec-testing-assignment-of-instance-variable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...