【问题标题】:Rails persisting data when using Devise/Chrome web driver/Capybara/FactoryGirlRails 在使用 Devise/Chrome web driver/Capybara/FactoryGirl 时持久化数据
【发布时间】:2015-03-13 17:49:07
【问题描述】:

我是一个相对较新的 Rails 开发人员,在测试我的应用时遇到了问题。我正在尝试测试设计注册功能,但是,我仍在获取持久数据。这是测试代码:

before(:all) do
 @state = FactoryGirl.create(:state)
 @brewery = FactoryGirl.create(:brewery, state_id: @state.id)
 @user = FactoryGirl.build(:user)
 @new_user = FactoryGirl.create(:user)
end

after(:all) do
 @state.destroy!
 @brewery.destroy!
 @user.destroy!
 @new_user.destory!
end


scenario "A user can create an account on Beerica", js: true do

 user_count = User.count


 visit new_user_registration_path

 fill_in 'Username', with: @user.username
 fill_in 'Email', with:  @user.email
 fill_in 'Password', with: @user.password
 fill_in 'Password confirmation', with: @user.password

 click_button 'Sign up'

 expect(page).to have_content('Welcome! You have signed up successfully.')
 expect(User.count).to eq(user_count + 1)
end

scenario "A user can add a brewery to their list of visited breweries", js: true do
 new_user = @new_user
 login_as(new_user, scope: new_user, run_callbacks: false)
 visit state_brewery_path(@state, @brewery)
 click_on "I visited this brewery"

 expect(user.breweries).to eq(1)
end

我将事务装置设置为 true,并且因为我使用 Chrome Web 驱动程序运行 Selenium,所以我必须使用 js: true。这里的 before all 块是我可以让数据通过浏览器运行的唯一方法,但我需要在之后将其删除。尽管如此,我相信 Devise 是两个用户在测试运行后坚持在我的测试数据库中的原因(导致电子邮件的唯一性错误)。这是我的工厂,应该会吸引新用户:

factory :user do
 sequence(:username) {|n| "baconpancakes#{n}"}
 sequence(:email) {|n| "baconpancakes#{n}@example.com"}
 password "makinbaconpancakes"
 password_confirmation "makinbaconpancakes"
end

谢谢!

【问题讨论】:

  • 我也知道 DatabaseCleaner 可能会有所帮助,但我需要它吗?

标签: ruby-on-rails selenium devise factory-bot


【解决方案1】:

before(:all) 在运行所有示例之前运行该块一次。

before(:each) 在文件中的每个规范之前运行该块一次

before(:all) 在所有 it 块运行之前将实例变量 @state、@brewery、@user、@new_user 设置一次。

但是, :before(:each) 会在每次运行 it 块时重置 before 块中的实例变量。

after(:all) 也是如此,因此您的测试用户不会被删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多