【发布时间】:2014-02-11 15:31:42
【问题描述】:
我的问题是我必须为每个单独的水豚测试创建一个新用户并登录。
一个例子如下:
require 'spec_helper'
describe "users" do
describe "user registration" do
it "should create a new user and log in" do
# Register a new user to be used during the testing process
visit signup_path
fill_in 'Email', with: 'testuser'
fill_in 'Password', with: 'testpass'
fill_in 'Password confirmation', with: 'testpass'
click_button 'Create User'
current_path.should == root_path
page.should have_content 'Thank you for signing up!'
end
end
describe "user login" do
it "should log in" do
# log in
visit login_path
fill_in 'Email', with: 'testuser'
fill_in 'Password', with: 'testpass'
click_button 'Log In'
current_path.should == root_path
page.should have_content 'Logged in!'
end
end
end
登录测试失败,因为该用户不再存在于该测试的数据库中。
这可以通过将两者都放在一个测试中来解决,但我认为这是不好的做法。
我还有另一个文件,目前正在使用 before_do 在每次测试之间注册和登录,这似乎也很糟糕......你can see that code here。
为了记录,这是我的第一个 Rails 应用程序,所以也许我试图以错误的方式做到这一点。我想尽可能地把它弄干..
在需要用户登录的页面上使用水豚真的这么糟糕吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 capybara rspec-rails