Ruby on rails Rspec测试未更新保存!从行动

Ruby on rails Rspec测试未更新保存!从行动,ruby-on-rails,ruby,rspec,rspec-rails,Ruby On Rails,Ruby,Rspec,Rspec Rails,在我的Rspec测试中,我正在检查我的JWT机密是否已旋转。但是,我的测试失败了,因为JWT令牌在我的测试中没有旋转,但在我的代码中有旋转 expect(Jwt.verify(token, user.jwt_secret)).to eq(true) # <--- Passes headers = { AUTHORIZATION: "JWT " + token } post("/logout", headers: headers) # <---

在我的Rspec测试中,我正在检查我的JWT机密是否已旋转。但是,我的测试失败了,因为JWT令牌在我的测试中没有旋转,但在我的代码中有旋转

expect(Jwt.verify(token, user.jwt_secret)).to eq(true) # <--- Passes
headers = { AUTHORIZATION: "JWT " + token }
post("/logout", headers: headers) # <--- calls logout action
expect(Jwt.verify(token, user.jwt_secret)).to eq(false) # <--- still returns true instead of expected false

单元测试应该只显式地测试代码中发生的事情。我建议您使用
注销测试关心的只是当前用户接收
:rotate\u jwt
。然后编写一个用户单元测试,测试
rorate\u jwt

差不多

it "calls rotate_jwt! on the current user" do
  allow(user).to receive(:rotate_jwt!).and_return(true) # don't actually call the method
  expect(user).to receive(:rotate_jwt!)

  post("/logout", headers: headers)
end

您的代码所做的一切都是在
用户上调用一个方法。仅进行测试。

我认为您需要调用
用户。在调用expect之前,重新加载
以刷新规范中的用户,由于controller中的当前用户不是规范中的用户,尽管两者都指向数据库中的同一条记录,即使rotate\u jwt更改了数据库中的某些内容,您仍然需要调用reload,因为rails会在您第一次调用user时缓存查询结果。jwt\u secret发生这种情况的原因是
主题
被存储,请检查以下说明:
it "calls rotate_jwt! on the current user" do
  allow(user).to receive(:rotate_jwt!).and_return(true) # don't actually call the method
  expect(user).to receive(:rotate_jwt!)

  post("/logout", headers: headers)
end