0

有人可以向我解释一下吗?怎么了?

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_at并由语句udated_at创建schema.rb_t.datetime
  • NetworkLatency 对象由 FactoryGirl 创建,如下所示

    @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)

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

    然后由控制器使用以下方法从数据库中获取:

    NetworkLatency.where(:from_network_id => site_ids).order('from_network_id ASC, to_network_id ASC')

4

5 回答 5

0

我遇到了同样的问题。我使用timecop gem 解决了。它会冻结时间,然后您可以在不干扰时间更改的情况下进行比较。

于 2015-01-22T12:45:02.943 回答
0

==您可以在您的cls中覆盖运算符NetworkLatency

def ==(other)
  self.from_network_id == other.from_network_id && self.to_network_id == other.to_network_id && ...
end
于 2013-07-30T20:00:35.393 回答
0

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

于 2013-07-30T19:43:09.557 回答
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
于 2013-07-30T19:43:19.257 回答
0

尝试

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

或者

expect(assigns(:latencies)).to match_array([@lat1, @lat2, @lat3])
于 2014-03-14T08:52:14.750 回答