我对测试相当陌生,并且有一个要测试的 UsersController。我从新动作开始,到目前为止有以下内容;
require 'spec_helper'
describe UsersController do
describe "GET 'new'" do
it "assigns a new User to @user" do
user = User.new
get :new
assigns(:user).should eq(user)
end
it "renders the :new template"
end
end
到目前为止,我的 UsersController 看起来像这样
class UsersController < ApplicationController
def new
@user = User.new
end
end
我希望我的第一个测试能够工作,但是当我运行它时,我得到以下结果;
Failures:
1) UsersController GET 'new' assigns a new User to @user
Failure/Error: assigns(:user).should eq(user)
expected: #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>
got: #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>
(compared using ==)
Diff:#<User:0x007fe4bbfceed0>.==(#<User:0x007fe4bce5c290>) returned false even though the diff between #<User:0x007fe4bbfceed0> and #<User:0x007fe4bce5c290> is empty. Check the implementation of #<User:0x007fe4bbfceed0>.==.
# ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
在控制台中玩耍会发现以下内容;
irb(main):001:0> a = User.new
=> #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>
irb(main):002:0> b = User.new
=> #<User id: nil, email: nil, username: nil, password_digest: nil, created_at: nil, updated_at: nil>
irb(main):003:0> a == b
=> false
所以现在我很好奇为什么 2 个空的 ActiveRecord 对象不相等(毕竟Array.new == Array.new
返回 true),以及我必须做些什么才能使我的测试通过。