这个简单的控制器示例
describe 'create' do
it 'creates a panel' do
panel = SimplePanel.make!
TestingGroup.any_instance.should_receive(:add_panel!).with(panel, [SampleType::SERUM])
post :create, {
submission_id: submission.to_param,
testing_group_id: testing_group.to_param,
sample_type_ids: [SampleType::SERUM],
panel_ids: [panel.id]
}
end
end
产生以下结果
Failure/Error: post :create, {
#<TestingGroup:0x00000006685910> received :add_panel! with unexpected arguments
expected: (, [1])
got: (#<SimplePanel id: 266, name: "Some Simple Panel", description: nil, type: "SimplePanel", active: true, test_type_id: 1, species_id: 1, autonomous_panel: true, related_panel_id: nil, lock_version: 0, created_at: "2013-09-02 06:08:08", updated_at: "2013-09-02 06:08:08">, ["1"])
我的问题是面板对象(第一个参数)似乎完全超出了测试结果的预期。
expected: (, [1])
它似乎不是 null 或空字符串,它只是消失了。在设置期望值之前立即输出面板对象的值并不令人意外,它是一个持久化到数据库的 ActiveRecord 对象。
将期望更改为:
TestingGroup.any_instance.should_receive(:add_panel!).with(instance_of(SimplePanel), [SampleType::SERUM])
产生以下结果
Failure/Error: post :create, {
#<TestingGroup:0x00000006ff82e0> received :add_panel! with unexpected arguments
expected: (#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x00000006f4a208 @klass=SimplePanel(id: integer, name: string, description: text, type: string, active: boolean, test_type_id: integer, species_id: integer, autonomous_panel: boolean, related_panel_id: integer, lock_version: integer, created_at: datetime, updated_at: datetime)>, [1])
got: (#<SimplePanel id: 268, name: "Some Simple Panel", description: nil, type: "SimplePanel", active: true, test_type_id: 1, species_id: 1, autonomous_panel: true, related_panel_id: nil, lock_version: 0, created_at: "2013-09-02 06:22:17", updated_at: "2013-09-02 06:22:17">, ["1"])
这似乎同样奇怪。有什么想法吗?
编辑:以下通常是由 SimplePanel.make 创建的!
attributes:
id: 2
name: Some Simple Panel
description:
type: SimplePanel
active: true
test_type_id: 1
species_id: 1
autonomous_panel: true
related_panel_id:
lock_version: 0
created_at: 2013-09-02 07:33:19.032650000 Z
updated_at: 2013-09-02 07:33:19.032650000 Z