0

这个简单的控制器示例

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
4

2 回答 2

1

您的第二个参数应该是[1]( Fixnum),而实际参数是["1"]( String)。也许第二个参数是您的规范失败的原因?

不过,这并不能解释您的第一个示例中的奇怪输出...

于 2013-09-02T06:59:33.207 回答
1

奇怪的输出是由于 SimplePanel 模型中的 description 属性造成的。当 RSpec 为其生成失败消息时,should_receive(..).with(..)它使用args.collect {|arg| arg.respond_to?(:description) ? arg.description : arg.inspect}.join(", "). 这会导致 中的空白expected( ,[1]),因为模拟模型上的描述是空白的。

编辑:此问题已在当前版本的 rspec 中得到修复 - https://github.com/rspec/rspec-mocks/pull/417

于 2013-09-09T18:12:33.247 回答