2013-03-09 57 views
0

我有一個Gemfile中:新來的Rails - 在集成測試工作不Webrat方法

 

     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 # 

你知道我在做什麼錯嗎?我只是捆綁安裝,並認爲它應該工作。 'click'方法之前的方法似乎以某種方式工作。

+0

你可以嘗試包括test_helper作爲一個文件嗎?即如果test_helper.rb位於同一個目錄中,則'require'。/ test_helper.rb'。 – 2013-03-09 08:23:47

+0

如果我寫: 需要 './test_helper' 我得到: '需要':無法加載這樣的文件 - ./test_helper(LoadError) 不知道我理解。 – wjandali 2013-03-09 09:17:14

+0

在這種情況下,嘗試像這樣 - '需要File.expand_path('./ test_helper.rb',__FILE __)''。另外,檢查test_helper和測試是否在同一個位置。否則,您需要操作此代碼以擁有正確的相對路徑。 – 2013-03-09 09:41:34

回答

1

只是因爲評論越來越囉嗦,我發佈這個答案。

Webrat Resources and references

在你的情況,因爲 '提交' 是一個按鈕,你需要使用

click_button("commit") 

代替

click("commit") 

如果它是一個鏈接,你可以使用

click_link("commit") 
+0

只是因爲這已經變得健談 - =] - 如果我問一個有點隨意的問題?我總是需要查找某些東西,但我有時會發現那些使用與所使用的寶石和框架相關的代碼來回答自己的問題的人。我是編程新手 - 如果我想達到這種熟練程度,您有任何建議嗎? – wjandali 2013-03-09 17:05:42

+1

@wjandali - 你的原始問題更容易回答:P。我相信每個人都需要注意事項,只是隨着時間的推移,會發生兩件事 - 你遇到類似的問題,或者你學習如何查找(提示 - 以'g'開頭)。只要你足夠執着,我認爲問題會得到解決。 – 2013-03-09 17:10:18