I receive the following error when I run my spec file:

enter image description here

I've read over the following questions to no avail:

I'm not sure that I've set up my Ruby project with rspec and factory_girl correctly. Here are the associated files:

player.rb

class Player
  attr_accessor :name

  def initialize(string)
    @name = string
  end
end

players.rb (factory)

require './player'

FactoryGirl.define do
  factory :player do
    name "Cliff Levingston"
  end
end

player_spec.rb

require 'spec_helper'

describe 'Player' do
  context 'when created' do
    it "should include a #name" do
      player1 = FactoryGirl.build :player
      # player1.name = "Gerald"
      expect(player1.name).to eql "Cliff Levingston"
    end
  end
end

Gemfile

source 'hhtps://rubygems.org'

gem 'guard'
gem 'guard-shell'
gem 'rspec'
gem 'factory_girl', '~> 4.0'
gem 'rb-fsevent', '~> 0.9'

Gemfile.lock

GEM
  remote: hhtps://rubygems.org/
  specs:
    activesupport (4.0.1)
      i18n (~> 0.6, >= 0.6.4)
      minitest (~> 4.2)
      multi_json (~> 1.3)
      thread_safe (~> 0.1)
      tzinfo (~> 0.3.37)
    atomic (1.1.14)
    celluloid (0.15.2)
      timers (~> 1.1.0)
    coderay (1.0.9)
    diff-lcs (1.2.5)
    factory_girl (4.3.0)
      activesupport (>= 3.0.0)
    ffi (1.9.3)
    formatador (0.2.4)
    guard (2.2.3)
      formatador (>= 0.2.4)
      listen (~> 2.1)
      lumberjack (~> 1.0)
      pry (>= 0.9.12)
      thor (>= 0.18.1)
    guard-shell (0.5.1)
      guard (>= 1.1.0)
    i18n (0.6.5)
    listen (2.2.0)
      celluloid (>= 0.15.2)
      rb-fsevent (>= 0.9.3)
      rb-inotify (>= 0.9)
    lumberjack (1.0.4)
    method_source (0.8.2)
    minitest (4.7.5)
    multi_json (1.8.2)
    pry (0.9.12.2)
      coderay (~> 1.0.5)
      method_source (~> 0.8)
      slop (~> 3.4)
    rb-fsevent (0.9.3)
    rb-inotify (0.9.2)
      ffi (>= 0.5.0)
    rspec (2.14.1)
      rspec-core (~> 2.14.0)
      rspec-expectations (~> 2.14.0)
      rspec-mocks (~> 2.14.0)
    rspec-core (2.14.7)
    rspec-expectations (2.14.4)
      diff-lcs (>= 1.1.3, < 2.0)
    rspec-mocks (2.14.4)
    slop (3.4.6)
    thor (0.18.1)
    thread_safe (0.1.3)
      atomic
    timers (1.1.0)
    tzinfo (0.3.38)

PLATFORMS
  ruby

DEPENDENCIES
  factory_girl (~> 4.0)
  guard
  guard-shell
  rb-fsevent (~> 0.9)
  rspec

spec_helper.rb

require 'rspec'
require 'factory_girl'

RSpec.configure do |config|
  # FactoryGirl
  FactoryGirl.find_definitions

  # Use color in STDOUT
  config.color_enabled = true

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  config.formatter = :documentation # :progress, :html, :textmate

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

I'd like to better understand what I omitted or need to add in order to remove the ArgumentError (0 for 1).

Thanks in advance.

有帮助吗?

解决方案

I believe the issue is with your player model requiring an argument at initialization.

Try this instead:

class Player
  attr_accessor :name

  def initialize(options={})
    @name = options[:name]
  end
end

When FactoryGirl initializes the model, it will initialize with a nil name attribute like this:

1.9.3p448 :013 > Player.new
#<Player:0x000000017acff0 @name=nil>

You can then define the name as you intend in your commented out line. This will also allow you to initialize a Player with a hash like:

1.9.3p448 :012 > Player.new(name: "something")
#<Player:0x00000003b53008 @name="something">
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top