【问题标题】:What does assigns mean in rspecrspec中的assigns是什么意思
【发布时间】:2013-04-21 16:02:32
【问题描述】:

那行代码有什么作用?

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

【问题讨论】:

    标签: ruby-on-rails rspec bdd


    【解决方案1】:

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


    在 cmets 中回答你的评论,我猜是:

    • 1) 您的索引操作看起来像 @articles = Articles.all(不过我希望您使用分页)

    • 2) 在上面的规范块之前,您已经在 db 中创建了一篇文章(或者我希望您在 db 中存根 db 查询)

    • 1 + 2 => @articles 应该包含一篇文章,这是您的规范期望

    【讨论】:

    • 我只是不明白它如何填充文章数组,以及我们如何检查它是否有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2017-06-11
    • 2018-03-05
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多