我在让Capybara与Rails一起工作时遇到了问题。只是在测试那个可能很有趣的东西。好的,在附加的代码中有几个等价的测试。第一个是用Rails附带的shoulda-context + Test::Unit创建的。第二个测试是用水豚做的,也应该是上下文。
require 'integration_test_helper'
class UsersTest < ActionDispatch::IntegrationTest
fixtures :all
context "signup" do
context "failure" do
setup do
@attr = { :name => "", :email => "", :password => "", :password_confirmation => "" }
end
should "not make a new user" do
assert_no_difference 'User.count' do
post_via_redirect "users", :user =>@attr # enviem les dades d'un nou usuari via create (POST /users)
assert_template 'users/new' # ens retorna a users/new, que significa que no s'ha creat l'usuari
assert_select "div#error_explanation" # comprovem que conte missatges d'error
end
end
should "not make a new user (capybara)" do
assert_no_difference 'User.count' do
visit '/signup'
fill_in 'Name', :with => @attr[:name]
fill_in 'Email', :with => @attr[:email]
fill_in 'Password', :with => @attr[:password]
fill_in 'Confirmation', :with => @attr[:password_confirmation]
click_button 'Sign Up!'
assert_template 'users/new' # ens retorna a users/new, que significa que no s'ha creat l'usuari
assert_select "div#error_explanation" # comprovem que conte missatges d'error
end
end
end
end虽然第一个工作正常,但水豚one抛出了这个错误消息:
================================================================================
Error:
test: signup failure should not make a new user (capybara). (UsersTest):
ArgumentError: @request must be an ActionDispatch::Request
test/integration/users_test.rb:30:in `block (4 levels) in <class:UsersTest>'
test/integration/users_test.rb:23:in `block (3 levels) in <class:UsersTest>'
================================================================================所需的*integration_test_helper.rb*文件是我在googling上搜索到的所有suposed解决方案的累加器,这些解决方案对我不起作用。
require 'test_helper'
require 'capybara/rails'
require 'database_cleaner'
# Transactional fixtures do not work with Selenium tests, because Capybara
# uses a separate server thread, which the transactions would be hidden
# from. We hence use DatabaseCleaner to truncate our test database.
DatabaseCleaner.strategy = :truncation
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# Stop ActiveRecord from wrapping tests in transactions
self.use_transactional_fixtures = false
teardown do
DatabaseCleaner.clean # Truncate the database
Capybara.reset_sessions! # Forget the (simulated) browser state
Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
end
end有人有解决方案吗?我是否应该尝试另一种集成框架,例如webrat?
我的设置是:
marcel@pua:~/Desenvolupament/Rails3Examples/ror_tutorial$ rake about
About your application's environment
Ruby version 1.9.2 (x86_64-linux)
RubyGems version 1.8.15
Rack version 1.3
Rails version 3.1.3
JavaScript Runtime therubyracer (V8)
Active Record version 3.1.3
Action Pack version 3.1.3
Active Resource version 3.1.3
Action Mailer version 3.1.3
Active Support version 3.1.3
Middleware ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000002b9bac0>, Rack::Runtime, Rack::MethodOverride, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::RemoteIp, Rack::Sendfile, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
Application root /mnt/dropbox/Dropbox/DESENVOLUPAMENT/Rails3Examples/ror_tutorial
Environment development
Database adapter sqlite3
Database schema version 20120127011330也是
shoulda-context (1.0.0)
capybara (1.1.2)谢谢
发布于 2012-02-02 08:13:41
您混淆了测试类型,并试图在错误的测试类型中断言模板。您应该只在功能测试中断言模板,在功能测试中,您只是直接测试控制器,而不是实际模拟用户交互。
Capybara是专门用于集成测试的,它本质上是从最终用户与浏览器交互的角度运行测试。在这些测试中,您不应该断言模板,因为最终用户无法看到应用程序的深层。相反,你应该测试的是一个动作是否让你走上了正确的道路。
current_path.should == new_user_path
page.should have_selector('div#error_explanation')请参阅git上Capybara自述文件中的“DSL”部分:https://github.com/jnicklas/capybara
发布于 2012-06-18 23:47:47
为了我的完整性,因为我知道我会一遍又一遍地回到这个链接:
那些使用测试单元和水豚的人,这也是一个很好的入门:from techiferous。
注意assert page.has_content?("something")的用法
对于测试路由来说,这和assert_equal some_path, current_path一样好用。
它是最完整的吗,不知道,但你不需要更多。
发布于 2012-02-03 01:28:46
感谢你的提示@Ryan。我试图弄清楚如何将一些RSpec集成测试示例从http://ruby.railstutorial.org/chapters/sign-up#sec:rspec_integration_tests转换为Test::Unit + Capybara。最初的RSpec集成测试是
it "should not make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => ""
fill_in "Email", :with => ""
fill_in "Password", :with => ""
fill_in "Confirmation", :with => ""
click_button
response.should render_template('users/new')
response.should have_selector("div#error_explanation")
end.should_not change(User, :count)
end
end因此,根据您的回答,我假设原始示例不应该包含response.should render_template('users/new')
https://stackoverflow.com/questions/9104915
复制相似问题