[英]Failing Rspec tests with Redis and Rails
我重构了Rails代码,将用户关系存储在Redis中,而不是Postgres数据库中。
之前的代码:
# user.rb
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :following, through: :relationships, source: :followed
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
重构代码:
# user.rb
def follow!(other_user)
rdb.redis.multi do
rdb[:following].sadd(other_user.id)
rdb.redis.sadd(other_user.rdb[:followers], self.id)
end
end
def following
User.where(id: rdb[:following].smembers)
end
重构的代码可以工作,但是我现有的规范现在失败了:
describe "following a user", js: true do
let(:other_user) { FactoryGirl.create(:user) }
before { visit user_path(other_user) }
it "should increment the following user count" do
expect do
click_button "Follow"
page.find('.btn.following')
end.to change(user.following, :count).by(1)
end
end
现在导致:
Failure/Error: expect do
count should have been changed by 1, but was changed by 0
Rspec使用不同的Redis数据库,该数据库在每个规范运行之前都会被刷新。 据我所知,该规范仍然应该通过。 我在这里想念什么吗?
to change(user.followers, :count).by(1)
应该改为
to change(other_user.followers, :count).by(1)
这可能是重装问题吗? 尝试这个:
it "should increment the following user count" do
expect do
click_button "Follow"
# user object is now stale, reload it from the DB
user.reload
end.to change(user.following, :count).by(1)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.