【发布时间】: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