【发布时间】:2020-06-07 00:18:42
【问题描述】:
我是学习 RSpec 的新手。我似乎无法理解为什么我的测试 #start method 失败了。
如果有人能给我解释一下,将不胜感激。
我得到的错误:
CardGame
attributes
should have a name
#response method
should say hello
#start method
can only implement class methods that are defined on a class (FAILED - 1)
Failures:
1) CardGame#start method can only implement class methods that are defined on a class
Failure/Error:
def initialize(name)
@name = name
end
ArgumentError:
wrong number of arguments (given 0, expected 1)
# ./lib/CardGame.rb:4:in `initialize'
# ./spec/class_double_spec.rb:29:in `block (3 levels) in <top (required)>'
Finished in 0.01178 seconds (files took 0.30252 seconds to load)
3 examples, 1 failure
Failed examples:
rspec ./spec/class_double_spec.rb:27 # CardGame#start method can only implement class methods that are defined on a class
➜ rspec-course
class_double_spec.rb
[ruby/spec/class_double_spec.rb]
require 'spec_helper'
require 'pry'
require './lib/CardGame'
require './lib/Deck'
describe CardGame do
let(:card) { instance_double(CardGame,
name: 'poker',
response: 'hello')}
let(:deck_klass) { class_double(Deck, build: ['Ace', 'Queen']).as_stubbed_const }
context 'attributes' do
it 'should have a name' do
expect(card.name).to eq('poker')
end
end
context '#response method' do
it 'should say hello' do
allow(card).to receive(:response).and_return('hello')
expect(card.response).to eq('hello')
end
end
context '#start method' do
it 'can only implement class methods that are defined on a class' do
expect(deck_klass).to receive(:build)
card.start
expect(card.cards).to eq(['Ace', 'Queen'])
end
end
end
CardGame.rb
[ruby/lib/CardGame.rb]
class CardGame
attr_accessor :name, :cards
def initialize(name)
@name = name
end
def response
'hello'
end
def start
@cards = Deck.build
end
end
甲板.rb
[ruby/lib/Deck.rb]
class Deck
def self.build
# business logic to build cards
end
end
【问题讨论】:
-
您使用什么版本的 RSpec?当我运行您的代码时,我收到其他错误`#
received unexpected message :start with (no args)`。 Rspec 3.9