简体   繁体   中英

Using assigns() in rspec view specs

I'm trying to model one of my view specs after the examples given here .

From my 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

This spec fails with:

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

I don't understand the failure... I'm doing exactly what the docs tell me to do. What am I missing?

You should use assign instead assigns :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM