我希望Capybara填写登录表单,点击“登录”,然后将我重定向到这样的根路径:(example)。这个应用程序是您使用Devise进行身份验证的经典博客应用程序 - 我无法想到任何会影响测试的其他内容。
我只得到一个失败的断言'测试错误所以我检查了我的按钮的拼写,用Warden进行了实验,但仍然无法弄明白。对于解决方案,我希望能够完全模仿用户在页面中的导航方式。
整合测试:
require 'test_helper'
require 'database_cleaner'
class PostsTest < ActionDispatch::IntegrationTest
# for setup and tear down each time we run tests
self.use_transactional_fixtures = false
setup do
DatabaseCleaner.start
end
teardown do
DatabaseCleaner.clean
end
def setup
@post = posts(:valid_post)
@admin = users(:james)
@regular = users(:steve)
end
test 'display comments' do
assert @post.comments.first.body == 'hello'
end
test 'visit contact page' do
visit '/contact'
assert page.has_text?('jamesyoun710')
end
test 'login as admin' do
visit new_user_session_path
assert page.has_field?('Email', type: 'email')
fill_in('Email', with: 'jamesyoun710@gmail.com')
fill_in('Password', with: 'gooneen44')
click_on('Log in') # this doesn't redirect me into the page
# login_as(@admin) # I need to do this to execute the line above
visit root_path
assert page.has_link?('Logout')
assert page.has_link?('New Post')
end
end
test_helper.rb中
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
require 'minitest/rails'
require 'capybara/rails'
require 'minitest/rails/capybara'
Minitest::Reporters.use!
# for creating sessions in tests
include Warden::Test::Helpers
Warden.test_mode!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
end
class ActionController::TestCase
include Devise::TestHelpers
end
class ActionDispatch::IntegrationTest
include Capybara::DSL
end
的Gemfile :
group :development, :test do
gem 'database_cleaner'
gem 'hirb'
gem 'byebug'
gem 'annotate'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'better_errors', '~> 2.1.1'
gem 'binding_of_caller', '~> 0.7.2'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'minitest-rails-capybara'
# gem 'mini_backtrace'
gem 'guard-minitest', '2.3.1'
end
答案 0 :(得分:0)
找到了解决方案 - 我本应该使用我的灯具(测试数据库)的登录信息,而是使用我的开发数据库中的登录信息。