匹配两个类时RSpec失败,即使这些类是_identical_

时间:2012-01-29 18:17:13

标签: ruby ruby-on-rails-3 rspec2 pandastream

我有以下代码检查返回的视频对象是否与我输入的视频对象相同。

视频对象是Panda Stream对象,并且在引用他们的主页时,Panda::Video.find应该使用Panda::Video.create方法完全返回我输入的对象

# screencast_spec.rb
before(:each) do
  @screencast = Factory.build(:screencast)
  @video = Panda::Video.create(:source_url => "http://panda-test-harness-videos.s3.amazonaws.com/panda.mp4")
end
it "should fetch the video with the #video method" do
  @screencast.video = @video
  @screencast.video.should == @video
end

# screencast.rb
# Returns the panda::video object
def video
  Panda::Video.find(self.video_id)
end
# Sets the +video_id+ reference
def video= video_object_or_id
  video_object_or_id = video_object_or_id.id unless video_object_or_id.is_a?(Integer)
  self.video_id = video_object_or_id
end

RSpec返回的错误是:

1) Screencast#video should fetch the video with the #video method
Failure/Error: @screencast.video.should == @video
   expected: #<Panda::Video id: "dac9bf057c9b667f57096054a64625a1", created_at: "2012/01/29 18:04:07 +0000", updated_at: "2012/01/29 18:04:07 +0000", original_filename: "panda.mp4", source_url: "http://panda-test-harness-videos.s3.amazonaws.com/panda.mp4", duration: nil, height: nil, width: nil, extname: ".mp4", file_size: nil, video_bitrate: nil, audio_bitrate: nil, audio_codec: nil, video_codec: nil, fps: nil, audio_channels: nil, audio_sample_rate: nil, status: "processing", path: "dac9bf057c9b667f57096054a64625a1", cloud_id: "55372b1612963b045f27bb093fed0abb">
        got: #<Panda::Video id: "dac9bf057c9b667f57096054a64625a1", created_at: "2012/01/29 18:04:07 +0000", updated_at: "2012/01/29 18:04:07 +0000", original_filename: "panda.mp4", source_url: "http://panda-test-harness-videos.s3.amazonaws.com/panda.mp4", duration: nil, height: nil, width: nil, extname: ".mp4", file_size: nil, video_bitrate: nil, audio_bitrate: nil, audio_codec: nil, video_codec: nil, fps: nil, audio_channels: nil, audio_sample_rate: nil, status: "processing", path: "dac9bf057c9b667f57096054a64625a1", cloud_id: "55372b1612963b045f27bb093fed0abb"> (using ==)
   Diff:
 # ./spec/models/screencast_spec.rb:26:in `block (3 levels) in <top (required)>'

注意差异说两个对象之间存在 no 差异,为什么比较失败?

如果您有任何指示,或者我已经尝试过测试panda stream之前我会感谢所有指导我

1 个答案:

答案 0 :(得分:0)

ruby​​提供了3种测试平等== eql? equals?的方法。使用==时,您将按对象标识进行比较。 ruby使用object-id。重要的是,类可以重新定义==,就像它为字符串比较所做的那样。

如果你想了解更多关于这个主题的信息,我可以建议你这篇博文:http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/