将Rspec与Factory Girl一起使用。尝试检查在 Controller 中分配了哪些数据(并对其进行测试)。我读过的每一篇文章都说我应该能够从assigns()中得到一些东西,但是它总是返回nill

控制者

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

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

输出量
foo =

我使用的语法错误吗?有一个更好的方法吗?谢谢!!

最佳答案

您必须先调用该 Action


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

关于ruby-on-rails - Rspec:测试实例变量的分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4720135/

10-09 06:56