运行规范文件时收到以下错误:
我把下列问题读了一遍,没有用:
ArgumentError: wrong number of arguments (0 for 1)
FactoryGirl issues - `factory': wrong number of arguments (0 for 1) (ArgumentError)
rspec not running because of factorygirl
ArgumentError in rspec
我不确定我是否正确地设置了与rspec和factory-girl的ruby项目。以下是相关文件:
RB

class Player
  attr_accessor :name

  def initialize(string)
    @name = string
  end
end

players.rb(工厂)
require './player'

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

球员规格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'

宝石锁
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

规范助手.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)而省略或需要添加的内容。
提前谢谢。

最佳答案

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

class Player
  attr_accessor :name

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

当初始化模型时,它将使用如下属性初始化:
1.9.3p448 :013 > Player.new
#<Player:0x000000017acff0 @name=nil>

然后,您可以在注释行中定义想要的名称。这还允许您使用如下散列初始化FactoryGirl
1.9.3p448 :012 > Player.new(name: "something")
#<Player:0x00000003b53008 @name="something">

关于ruby - Ruby off Rails FactoryGirl ArgumentError:args的数量错误(0表示1),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19937719/

10-12 12:28