Rspec:错误应该是eq()

时间:2013-07-30 19:14:47

标签: ruby-on-rails-3 rspec factory-bot rspec-rails

有人可以向我解释一下吗?有什么问题?

1) NetworkController GET #index for staff user locates all network latencies to display
 Failure/Error: assigns(:latencies).should eq([@lat1, @lat2, @lat3])

   expected: [#<NetworkLatency from_network_id: "BSC", to_network_id: "FZJ", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">, #<NetworkLatency from_network_id: "CSC", to_network_id: "ABC", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">, #<NetworkLatency from_network_id: "CSC", to_network_id: "DEF", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">]
        got: [#<NetworkLatency from_network_id: "BSC", to_network_id: "FZJ", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">, #<NetworkLatency from_network_id: "CSC", to_network_id: "ABC", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">, #<NetworkLatency from_network_id: "CSC", to_network_id: "DEF", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">]

   (compared using ==)

   Diff:
   @@ -1,4 +1,2 @@
   -[#<NetworkLatency from_network_id: "BSC", to_network_id: "FZJ", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">,
   - #<NetworkLatency from_network_id: "CSC", to_network_id: "ABC", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">,
   - #<NetworkLatency from_network_id: "CSC", to_network_id: "DEF", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">]
   +[#<NetworkLatency from_network_id: "BSC", to_network_id: "FZJ", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">, #<NetworkLatency from_network_id: "CSC", to_network_id: "ABC", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">, #<NetworkLatency from_network_id: "CSC", to_network_id: "DEF", error_status: nil, percent_packet_lost: 0, min_ping: 54.8, avg_ping: 54.8, max_ping: 54.8, created_at: "2013-07-30 19:09:14", updated_at: "2013-07-30 19:09:14">]

Expectedgot等于我。那么为什么Rspec报告错误?

修改

  • 我使用eq
  • created_atudated_atschema.rb语句<{1}}创建
  • NetworkLirlncy对象由FactoryGirl创建,如下所示

    t.datetime

    @lat1 = FactoryGirl.create(:network_latency, from_network: @bsc_net, to_network: @fzj_net)

    @lat2 = FactoryGirl.create(:network_latency, from_network: @csc_net, to_network: @abc_net)

    然后由控制器使用以下命令从db获取:

    @lat3 = FactoryGirl.create(:network_latency, from_network: @csc_net, to_network: @def_net)

5 个答案:

答案 0 :(得分:0)

测试结果表明两个数组的字符串表示相同,但​​这并不意味着数组彼此eq,这要求每个元素都是eq到另一个数组中的相应元素。你知道每个数组中的对象是一样的吗?如果没有,您是否知道对象是否具有==的非标准定义?

答案 1 :(得分:0)

这些时间类型实际上是字符串,还是Ruby的时间类型?后者包括在字符串表示中不可见的毫秒和微秒;这使得字符串表示相等而时间可能不同。

例如:

2.0.0p0 :001 > Time.now == Time.now
 => false
2.0.0p0 :002 > Time.now.to_s == Time.now.to_s
 => true
2.0.0p0 :003 > puts "%s, %s" % [ Time.now.tv_usec, Time.now.tv_usec]
 247806, 247810

答案 2 :(得分:0)

您可以在== clss:

中覆盖NetworkLatency运算符
def ==(other)
  self.from_network_id == other.from_network_id && self.to_network_id == other.to_network_id && ...
end

答案 3 :(得分:0)

assigns(:latencies).should match_array([@lat1, @lat2, @lat3])

expect(assigns(:latencies)).to match_array([@lat1, @lat2, @lat3])

答案 4 :(得分:0)

我遇到了同样的问题。我使用timecop gem解决了问题。它冻结了时间,然后你可以进行比较,而不会干扰时间的变化。