在rspec视图规范中使用assigns()

时间:2015-09-18 12:51:17

标签: ruby-on-rails rspec

我试图在给出here的示例后对我的一个视图规范进行建模。

来自我的Gemfile:gem 'rspec-rails', '~> 3.0'

# /app/spec/views/teams/index.html.haml_spec.rb
RSpec.describe "teams/index", type: :view do

  before (:each) do
    assigns(:teams, [
      FactoryGirl.create(:team, name: "Team 1"),
      FactoryGirl.create(:team, name: "Team 2")
    ])
  end

  it "renders a list of teams" do
    render

    expect(rendered).to match /Team 1/
    expect(rendered).to match /Team 2/
  end
end

此规范失败了:

 1) teams/index renders a list of teams
     Failure/Error: assigns(:teams, [
     ArgumentError:
       wrong number of arguments (2 for 0..1)

我不理解失败......我正在完成文档告诉我要做的事情。我错过了什么?

1 个答案:

答案 0 :(得分:3)

您应该使用分配而不是分配

before (:each) do
  assign(:teams, [
    FactoryGirl.create(:team, name: "Team 1"),
    FactoryGirl.create(:team, name: "Team 2")
  ])
end