允许消息
测试替身 默认情况下是“严格的” - 未明确允许或预期的消息将触发错误。 使用 allow(...).to receive(...)
配置替身允许接收的消息。 您也可以使用 allow(...).to receive_messages(...)
批量配置允许的消息(和返回值)。
允许的消息默认情况下返回 nil
给定一个名为“allowmessagespec.rb” 的文件,其中包含
RSpec.describe "allow" do
it "returns nil from allowed messages" do
dbl = double("Some Collaborator")
allow(dbl).to receive(:foo)
expect(dbl.foo).to be_nil
end
end
当我运行 rspec allow_message_spec.rb
那么所有示例都应该通过。
可以使用 receive_messages
批量允许消息
给定一个名为“receivemessagesspec.rb” 的文件,其中包含
RSpec.describe "receive_messages" do
it "configures return values for the provided messages" do
dbl = double("Some Collaborator")
allow(dbl).to receive_messages(:foo => 2, :bar => 3)
expect(dbl.foo).to eq(2)
expect(dbl.bar).to eq(3)
end
end
当我运行 rspec receive_messages_spec.rb
那么所有示例都应该通过。