Ruby on rails rspec应接收(x)。with(参数)缺失
这是一个简单的控制器示例Ruby on rails rspec应接收(x)。with(参数)缺失,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,这是一个简单的控制器示例 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,
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"])
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"])
它看起来不是null,也不是空字符串,它只是消失了。输出面板对象的值
在设置期望值之前,它是一个被激活的ActiveRecord对象
保存到数据库
将期望更改为:
TestingGroup.any_instance.should_receive(:add_panel!).with(instance_of(SimplePanel), [SampleType::SERUM])
生成以下结果
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"])
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"])
第二个参数应该是
[1]
(Fixnum
),而实际参数是[“1”]
(String
)。也许第二个参数是什么不符合你的规格
这并不能解释第一个示例中的奇怪输出…奇怪输出是由于SimplePanel模型中的description属性造成的。当RSpec为
应该接收(..)生成失败消息时,使用(..)
它通过使用args.collect{arg | arg.respond_to?(:description)?arg.description:arg.inspect}.join(,)来格式化参数。这导致了在<代码>预期([1)] < /代码>中的空白,因为对模拟模型的描述是空白的。
编辑:此问题已在当前版本的rspec中修复-您能否显示panel=SimplePanel.make的示例值代码>?请看我上面的编辑。除了奇怪的输出,这就是问题所在。一旦断言更改为:TestingGroup.any_instance.should_receive(:add_panel!),并带有(SimplePanel的instance_),[“#{SampleType::SERUM}]”
测试通过。我的假设(在睡了一个好觉之后)是,它永远不会以最初的预期通过,因为控制器操作加载的面板对象(尽管是同一个数据库记录)是不同的ActiveRecord对象,因此它无论如何都不会匹配。第二个参数中的字符串vs Fixnum问题是导致测试失败的原因。