3

运行我的规范文件时收到以下错误:

在此处输入图像描述

我已阅读以下问题无济于事:

我不确定我是否使用 rspec 和 factory_girl 正确设置了我的 Ruby 项目。以下是相关文件:

播放器.rb

class Player
  attr_accessor :name

  def initialize(string)
    @name = string
  end
end

player.rb(工厂)

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

宝石文件

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

我想更好地了解我省略或需要添加的内容以删除 ArgumentError(0 表示 1)。

提前致谢。

4

1 回答 1

1

我认为问题在于您的播放器模型在初始化时需要一个参数。

试试这个:

class Player
  attr_accessor :name

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

初始化模型时FactoryGirl,它将使用nil name如下属性进行初始化:

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

然后,您可以在注释行中定义您想要的名称。这也将允许您Player使用如下哈希初始化 a:

1.9.3p448 :012 > Player.new(name: "something")
#<Player:0x00000003b53008 @name="something">
于 2013-11-12T19:10:16.540 回答