2013-10-08 93 views
0

嗨,我想通過水豚填充一些數據來測試表格。我的測試運行時沒有任何錯誤,但我無法在測試數據庫或開發數據庫中看到該數據。我的測試是在「spec/feature/FILE NAME」中。我的測試有點像水豚rspec不工作

require 'spec_helper' 

    feature "New Application" do 
    scenario 'has 200 status code if logged in' do 
    visit '/applications/new?id=.........' 
    fill_in 'application[applicants_attributes][0][first_name]', :with => 'Rose' 
    fill_in 'application[applicants_attributes][0][first_name]', :with => 'Farmer' 
    click_link 'sbmt' 
    current_path.should == '/applications/new' 
    page.status_code.should be 200 
    end  
    end 

需要幫助!!!! 我spec_helper是一樣的東西

require 'simplecov' 
SimpleCov.start do 
    add_filter '/spec/' 
    add_filter '/config/' 
    add_filter '/lib/' 
    add_filter '/vendor/' 
end 
# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 

# Add this to load Capybara integration: 
require 'capybara/rspec' 
require 'capybara/rails' 
require 'rspec/autorun' 
require 'crack' 

Capybara.register_driver :rack_test do |app| 
    Capybara::RackTest::Driver.new(app, :headers => { 'User-Agent' => 'Capybara' }) 
end 
# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 

RSpec.configure do |config| 
    config.include Capybara::DSL, type: :feature 


    RSpec.configure do |config| 
    config.use_transactional_fixtures = false 
    #config.use_transactional_fixtures = false 

    config.before :each do 
    DatabaseCleaner.strategy = :truncation 
    DatabaseCleaner.start 
    end 

    config.after do 
    DatabaseCleaner.clean 
    end 
end 

    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Run specs in random order to surface order dependencies. If you find an 
    # order dependency and want to debug it, you can fix the order by providing 
    # the seed, which is printed after each run. 
    #  --seed 1234 
    config.order = "random" 
end 

我的RSpec的輸出

Failures: 

    1) New Application has 200 status code if logged in 
    Failure/Error: Application.where("first_name = 'Rose' AND last_name = 'Farmer'").count.should == 1 
     expected: 1 
      got: 0 (using ==) 
    # ./spec/features/applications_controller_spec.rb:23:in `block (2 levels) in <top (required)>' 

Finished in 0.7381 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/features/applications_controller_spec.rb:4 # New Application has 200 status code if logged in 

Randomized with seed 49732 

Coverage report generated for Cucumber Features to /home/nomi/homesbyhaven/coverage. 241/616 LOC (39.12%) covered. 

問題是我不能夠將數據保存到數據庫中。如果我從測試中刪除應用程序表中數據的檢查。它通過。但我怎麼能證實它確實通過了。我的意思是說沒有問題。

謝謝

+0

您是否啓用了transactional_fixtures?如果是這樣,所有的交易將在測試結束時回滾 – usha

+0

我試過禁用它們,但我有 「未定義的方法'use_transactional_fixtures ='for#( NoMethodError)「 – nOmi

+0

發佈您的rspec輸出!我們不知道你的問題的症狀是什麼。你看不到數據庫中的數據,或者只是你的測試失敗了? – Kosmonaut

回答

1

我覺得這裏的問題是,你正在測試的錯誤的事情。功能(集成)規格以用戶與之交互的方式測試系統。因此,儘可能地,您的測試應該限制在用戶可以執行的活動中。例如,在添加用戶之後,不要檢查這些內容,而是要讓用戶執行相同的操作來驗證操作是否成功。您可以訪問用戶頁面以查看該用戶是否已添加,或者在頁面上有一個元素來告訴您存在多少用戶。

請記住,測試服務器和瀏覽器使用單獨的進程。如果您希望服務器在一個進程中完成某個操作(例如,添加一個用戶),但是數據庫更改正在另一個進程中發生,則這可能會導致計時問題。您可以通過讓測試瀏覽器模仿用戶的操作來避免這些問題。在模型規格中進行數據庫測試。

0

默認情況下,每次測試後清除測試數據庫。所以每次你運行測試時,你都會有一個乾淨的石板。

如果你不希望這樣的事情發生,你可以改變配置在你的投機/ spec_helper.rb到

config.use_transactional_fixtures = false 

,但如果你不是每次測試後清除數據庫,然後從創建的任何記錄以前運行的測試可能會改變下一次測試的結果。給你一個假陽性或陰性

+0

謝謝你的答案。但我越來越 未定義的方法'use_transactional_fixtures ='爲#(NoMethodError) 並且我在數據庫中沒有記錄。 – nOmi

0

你在spec/spec_helper.rb中會有一個像下面的塊。我不建議禁用事務性夾具,因爲當您有多個測試時它會導致很多問題。

RSpec.configure do |config| 
    # Mock Framework 
    config.mock_with :rspec 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # Render views when testing controllers 
    # This gives us some coverages of views 
    # even when we aren't testing them in isolation 
    config.render_views 
    end 

你可以做相反的是,使數據庫支票斷言在你的測試用例

require 'spec_helper' 

    feature "New Application" do 
    scenario 'has 200 status code if logged in' do 
     visit '/applications/new?id=.........' 
     fill_in 'application[applicants_attributes][0][first_name]', :with => 'Rose' 
     fill_in 'application[applicants_attributes][0][last_name]', :with => 'Farmer' 
     click_link 'sbmt' 
     current_path.should == '/applications/new' 
     Application.where("first_name = 'Rose' AND last_name = 'Farmer'").count.should == 1 
     page.status_code.should be 200 
    end  
    end 

如果上述測試失敗,那麼按照預期的功能不能正常工作

如果您使用的不是ActiveRecord,則無法使用事務性夾具。 如果您正在使用MongoID可以使用Database Cleaner帶截斷戰略

RSpec.configure do |config| 
    config.use_transactional_fixtures = false 

    config.before :each do 
    DatabaseCleaner.strategy = :truncation 
    DatabaseCleaner.start 
    end 

    config.after do 
    DatabaseCleaner.clean 
    end 
end 
+0

我仍然得到「未定義方法transactional_fixtures」 :( 但如果我刪除了這一行就測試失敗 – nOmi

+0

你是怎麼測試之前通過? – usha

+0

你不使用ActiveRecord? – usha