我有一个 Gemfile:
source 'https://rubygems.org' gem 'rails', '3.2.11' gem 'omniauth' gem 'omniauth-facebook' gem 'thin' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' gem 'pg' gem 'devise' gem 'rmagick' # Because rails_admin_jcrop autoload modules by checking plugins you use, it's # recommended to require it explictly before rails_admin_jcrop # e.g. if you use carrierwave gem 'carrierwave', :require => 'carrierwave' # Gems used only for assets and not required # in production environments by default. group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' gem 'compass-rails' gem 'zurb-foundation' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.0.3' end group :test do gem 'webrat', '>=0.7.2.pre', :git => 'http://github.com/kalv/webrat.git' gem "database_cleaner" end gem 'jquery-rails' # To use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' # To use Jbuilder templates for JSON # gem 'jbuilder' # Use unicorn as the app server # gem 'unicorn' # Deploy with Capistrano # gem 'capistrano' # To use debugger # gem 'debugger' gem 'therubyracer'
和 test_helper.rb:
ENV["RAILS_ENV"] = "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' # require "webrat" Webrat.configure do |config| config.mode = :rails end class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # include Webrat include Webrat::Methods include Webrat::Matchers # Add more helper methods to be used by all tests here... end class ActionController::TestCase include Devise::TestHelpers end
我的测试是:
require 'test_helper' class UserSignupTest user.first_name fill_in "user_last_name", :with => user.last_name fill_in "user_username", :with => user.username fill_in "user_email", :with => user.email fill_in "user_password", :with => user.password fill_in "user_password_confirmation", :with => user.password_confirmation choose("user_sex_male") click("commit") end end
但是当我尝试使用“点击”方法时出现以下错误:
1) Error: test_sign_up_flow(UserSignupTest): NoMethodError: undefined method `click' for #
你知道我做错了什么吗?我只是捆绑安装并认为它应该可以工作。 “点击”方法之前的方法似乎以某种方式起作用。
最佳答案
只是因为评论越来越多,所以我将其作为答案发布。
Webrat Resources and references .
在您的情况下,由于“提交”是一个按钮,您需要使用
click_button("commit")
代替
click("commit")
如果它是一个链接,你可以使用
click_link("commit")
关于ruby-on-rails - Rails 新手——Webrat 方法在集成测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15308390/