这是一个非常简单的问题,但我找不到任何解决方案。已经3天了。请帮忙。
所以我有这个
用户 Controller .rb
def index
@users = @users.normal.order(:name).page params[:page]
end
所以我在我的 users_controller_spec.rb 中有这个
it "assign all normal users to @users" do
users = User.normal.all
get :index
expect(assigns(:users)).to eq(users)
end
我的代码基于 https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs .
在我的终端中运行 rspec 后,这是输出。
Diff:
@@ -1,4 +1,4 @@
-[#<User:0x00000004aa32b0
+[#<User:0x00000004a085a8
id: 2,
name: "Christian Galamay",
admin: false,
@@ -20,7 +20,7 @@
updated_at: Wed, 13 Apr 2016 01:46:17 UTC +00:00,
role: "Normal",
avatar: nil>,
- #<User:0x00000004aa2ea0
+ #<User:0x000000049dbd78
id: 3,
name: "Grace Sojor",
admin: false,
@@ -42,7 +42,7 @@
updated_at: Wed, 13 Apr 2016 01:46:17 UTC +00:00,
role: "Normal",
avatar: nil>,
- #<User:0x00000004aa2ab8
+ #<User:0x000000049db760
id: 4,
name: "Karl Pandacan",
admin: false,
输出表明所有值都相同,除了用户后面的十六进制。所以我的问题是(1)为什么在比较两个ActiveRecord时包含User之后的十六进制(2)是否有任何方法或函数可以用来忽略User中的十六进制。谢谢你
最佳答案
您可以简单地检查用户数组而不是事件记录关系
expect(assigns(:users).to_a).to eq(users.to_a)
关于ruby-on-rails - 索引中 instance_variable 的 RSpec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36587311/