1

私は Rails の初心者です。助けてください。何が問題なのかわかりません。前に何か他のものが書かれているようです(:each)...そして、Cardオブジェクトが作成されていないtest_spac.rbにテストを書くにはどうすればよいですか。メソッド before(:each) は、このテストでは機能しませんでした。

これは仕事です:

test_spec.rb:

require 'rails_helper'

describe "Index Page" do

  before(:each) do
    @card = FactoryGirl.create(:card)
    DatabaseCleaner.start
  end

  it "shows error messages if translation is wrong" do
    visit root_path
    fill_in 'translated_text', with: 'wqww'
    click_on 'Проверить'
    expect(page).to have_content('Неверно!')
  end

  after(:each) do
    DatabaseCleaner.clean
  end

end

rails_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'database_cleaner'
Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :poltergeist


Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = false

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each, type: :features) do
    DatabaseCleaner.strategy = :truncation
  end

  config.infer_spec_type_from_file_location!
end

これは機能しません:

test_spec.rb:

require 'rails_helper'

describe "Index Page" do

  before(:each) do
    @card = FactoryGirl.create(:card)
  end

  it "shows error messages if translation is wrong" do
    visit root_path
    fill_in 'translated_text', with: 'wqww'
    click_on 'Проверить'
    expect(page).to have_content('Неверно!')
  end

end

rails_helper.rb:

config.use_transactional_fixtures = false

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each, type: :features) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
        DatabaseCleaner.start
      end

  config.after(:each) do
        DatabaseCleaner.clean
      end
4

2 に答える 2

0

働いていないということの意味を投稿できますか?エラーメッセージは表示されますか?

ところで、なぜconfig.use_transactional_fixtures障害者になったのですか?フックの使用に固執する場合before(:each)、RSpec がデータベースをリセットするので、DatabaseCleaner.

于 2014-08-07T11:42:51.127 に答える