What does assigns mean in rspec

做~自己de王妃 提交于 2019-12-06 17:04:13

问题


What does that line of code do?

assigns(:articles).should eq([article])

in the following rspec

  describe "GET #index" do
    it "populates an array of articles" do
      article = Factory(:article)
      get :index
      assigns(:articles).should eq([article])
    end

    it "renders the :index view" do
      get :index
      response.should render_template :index
    end
  end

回答1:


assigns relates to the instance variables created within a controller action (and assigned to the view).


to answer your remark in comments, I guess that:

  • 1) your index action looks like @articles = Articles.all (I hope you use pagination though)

  • 2) prior to the spec block above, you have one article created in db (or I hope you stub db queries in db)

  • 1 + 2 => @articles should contain one article, that's your spec expectation



来源:https://stackoverflow.com/questions/16133166/what-does-assigns-mean-in-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!