我想通过一个测试,将一个项目从另一个类中推入一个数组。
require_relative 'message'
class Test
attr_reader :message
def initialize(message = Message.new)
@message = message
end
def push(hello)
@message.array << hello
end
end
在另一个类中的空数组。
class Message
attr_reader :array
def initialize
@array = []
end
end
而我的测试。
require 'test'
describe Test do
let(:message) { double(array: [])}
describe '#push' do
it 'pushes an item into an array from the message class' do
subject.push("hello")
expect(message.array).to eq ["hello"]
end
end
end
目前得到的错误是
expected: ["hello"]
got: []
(compared using ==)
我做错了什么?这个方法本身很简单,也能用,为什么我的测试不能用?
这个 message
你在这里定义。let(:message) { double(array: [])}
和其他的没有任何关系。
既然你推到了 subject
你必须检查它。
require 'test'
describe Test do
describe '#push' do
it 'pushes an item into an array from the message class' do
subject = Test.new
subject.push("hello")
expect(subject.message.array).to eq ["hello"]
end
end
end
在你的let中,你给message分配一个空数组,然后测试它是否包含'hello'。这样做失败了,因为 message 是空的,你的意思是把'hello'推送给 message 数组而不是'subject'?你的意思是将'hello'推送到消息数组而不是'subject'?