我有一个用户模型,其中包含下一个和上一个用户的方法:
def next_user
User.where("id > ?", id).order("id ASC").first
end
def prev_user
User.where("id < ?", id).order("id DESC").first
end
我正在尝试使用 RSpec 和 Factory girl 来设置测试来测试它,但我不确定该方法 - 我只是开始使用 TDD。
我正在尝试这个:
describe "previous_next" do
let(:user) { FactoryGirl.create(:user) }
let(:next_user) { FactoryGirl.create(:user) }
it "should know the next user" do
expect(user.next_user).to eq next_user
end
it "should know the previous user" do
expect(next_user.prev_user).to eq user
end
end
这是错误
1) User previous_next should know the next user
Failure/Error: expect(user.next_user).to eq next_user
expected: #<User id: 7624, name: "Example User", email: "sample9@email.com", created_at: "2013-01-13 04:31:19", updated_at: "2013-01-13 04:31:19", password_digest: "$2a$04$mbzI.yXYd9eSSfXbjChROewwvCHLFQI6qq5IUNzAKo9O...", remember_token: "KMSmiEeOr6f_Sgi8KJffIA", admin: false, plan_id: nil, password_reset_token: nil, password_reset_sent_at: nil, stripe_customer_token: nil>
got: nil
(compared using ==)
# ./spec/models/user_spec.rb:93:in `block (3 levels) in <top (required)>'
2) User previous_next should know the previous user
Failure/Error: expect(next_user.prev_user).to eq user
expected: #<User id: 7626, name: "Example User", email: "sample11@email.com", created_at: "2013-01-13 04:31:19", updated_at: "2013-01-13 04:31:19", password_digest: "$2a$04$uzrKmsXmVY7jUdRtfnfhUe89Jgy1176zE2UxdH90imJR...", remember_token: "gl10QvxjSMZYQS3JIgbREg", admin: false, plan_id: nil, password_reset_token: nil, password_reset_sent_at: nil, stripe_customer_token: nil>
got: nil
(compared using ==)
# ./spec/models/user_spec.rb:97:in `block (3 levels) in <top (required)>'
如果我将 next_user 记录创建为 :user Factory 的实例,为什么它会评估为零?