rspec中的赋值是什么意思 [英] What does assigns mean in rspec

查看:75
本文介绍了rspec中的赋值是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该行代码有什么作用?

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

在以下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

推荐答案

assigns与在控制器动作(创建并分配给视图)中创建的实例变量相关.

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)您的索引操作类似于@articles = Articles.all(但我希望您使用分页)

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

2)在上面的spec块之前,您已经在db中创建了一篇文章(或者希望您在db中保留db查询)

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应该包含一篇文章,这是您的规格期望

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

这篇关于rspec中的赋值是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆