簡體   English   中英

水豚與RSpec無法正常工作

[英]Capybara with rspec not working

嗨,我正在嘗試通過水豚填充一些數據來測試表格。 我的測試運行沒有任何錯誤,但是我在測試數據庫或開發數據庫中都看不到該數據。 我的測試在“規范/功能/文件名”中。 我的測試就像

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.

問題是我無法將數據保存到數據庫。 如果我從測試中刪除應用程序表中的數據檢查。 它通過了。 但是我如何驗證它真的通過了。 我的意思是說沒有問題。

謝謝

我認為這里的問題是您正在測試錯誤的東西。 功能(集成)規范對用戶與系統交互的方式進行測試。 因此,您的測試應盡可能地限制於用戶可以執行的活動。 例如,在添加用戶之后,無需檢查用戶名User.count增加一, User.count讓測試執行用戶將執行的相同操作來驗證操作是否成功。 您可以訪問用戶頁面以查看是否添加了該用戶,或在頁面上具有一個元素,該元素可以告訴您存在多少用戶。

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

默認情況下,每次測試后都會清除測試數據庫。 這樣,每次運行測試時,您的表盤就會干凈整潔。

如果您不希望發生這種情況,可以將spec / spec_helper.rb中的配置更改為

config.use_transactional_fixtures = false

但是,如果您沒有在每次測試后清除數據庫,則從先前運行的測試創建的任何記錄都可能會更改下一個測試的結果。 給您虛假的肯定或否定

您的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,則可以使用帶有截斷策略的數據庫清理程序

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM