Rspec Factory 问题-概念问题?
我正在尝试在 Ruby on Rails 系统中测试一个案例,在该系统中,我们在 x 次登录尝试失败后将用户锁定。我遇到的问题是尝试创建的用户已达到“锁定”其帐户的数量。我正在使用 Factories 创建一个像这样的用户 -
Factory.define :locked_user, :class => User do |user|
user.name "Test Lock"
user.email "[email protected]"
user.password "blah1234"
user.password_confirmation "blah1234"
user.login_count 5
end
其中 5 是“幻数”。当我尝试使用类似的东西时,
@user = Factory(:locked_user)
它会在数据库中创建一个用户 - 但新创建的用户总是将 login_count 设置为零,因此它只会将他记录在测试中。当我尝试像这样的 .build 方法时,
@user = Factory.build(:locked_user)
它会像我想要的那样设置一个带有 login_count = 5 的用户,但随后不会将该用户视为有效并且不会尝试登录它们(即,它给了我们“坏用户”) /密码”错误而不是“正确的用户/密码,但您被锁定”错误)。我想我在这里遗漏了一些东西,无法让 RSpec 识别出这是有效用户但帐户应该被锁定的事实。有人可以帮我纠正吗?以下是整个描述块 -
describe "with locked account" do
before(:each) do
@user = Factory.build(:locked_user)
@attr = { :email => @user.email, :password => @user.password}
end
it "should not allow signin with locked account" do
post :create, :session => @attr
flash.now[:error].should =~ /Invalid user locked out/i
end
end
I'm trying to test a case in our Ruby on Rails system where we lock a user out after x failed login attempts. The issue I'm having is trying to create a user has reached the number that 'locks' his account. I am using Factories to create a user like so-
Factory.define :locked_user, :class => User do |user|
user.name "Test Lock"
user.email "[email protected]"
user.password "blah1234"
user.password_confirmation "blah1234"
user.login_count 5
end
Where 5 is the 'magic number'. When I try to use something like
@user = Factory(:locked_user)
It creates a user in the database- but newly created users always have login_count set to zero, so it just logs him in the test. When I try the .build method like so
@user = Factory.build(:locked_user)
It sets a user with login_count = 5 like I want, but then doesn't see the user as valid and won't try to log them in (ie, it gives us the 'bad user/password' error rather then 'right user/password but you are locked out' error). I guess I'm missing something here to get RSpec to pick up the fact that this is valid user but the account should be locked. Can someone help set me straight? Below is the entire desribe block-
describe "with locked account" do
before(:each) do
@user = Factory.build(:locked_user)
@attr = { :email => @user.email, :password => @user.password}
end
it "should not allow signin with locked account" do
post :create, :session => @attr
flash.now[:error].should =~ /Invalid user locked out/i
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您在创建用户后设置 login_count ,或者存根告诉您用户登录是否被锁定的方法。
例如,在创建用户后使用 update_attribute 强制登录计数:
或者使用存根来存根locked_login?或等效方法:
I would recommend you either set the login_count after creating the user, or stub the method that tells you if a user login is locked.
For instance, use update_attribute to force the login_count after the user has been created:
Or use stubs to stub out the locked_login?, or equivalent method: