0

Rails は初めてで、現在、送信フォームのテストを作成しようとしています。フォームが Deal モデルの新しいレコードを作成できるかどうかをテストすることになっています。どうやら私が作成しようとしている Community オブジェクトが実際にはドロップダウンに表示されないため、失敗しています。なんらかの理由でレコードが作成されていないのか、それとも永続化されていないのか疑問に思っています。

私が得ているエラーは

Failure/Error: select community.name,   :from => "deal[community_id]"
Capybara::ElementNotFound:
Unable to find option "Community7"

ここに私の仕様の関連部分があります:

describe "Deal Pages" do

  subject {page}

  describe "create Deal" do
    let(:user) { FactoryGirl.create :user, username: "user_1", email: "user_1@email.com", password: "password" }
    let(:community) { FactoryGirl.create :community, name: "Community7", title: "Community 7", description: "A great community", user: user }

    before(:each) do
      sign_in user
      visit new_deal_path
    end

    let(:submit) { "Submit Deal" }

    describe "with invalid information" do

      it "should not create a Deal" do
        expect { click_button submit }.not_to change(Deal, :count)
      end

      describe "after submission" do
        before { click_button submit }

        it { should have_title('Submit A New Deal') }
        it { should have_content('error') }
      end
    end

    describe "with valid information" do

      it "should create a Deal" do
        fill_in "Title",         with: "A New Deal"
        select community.name,  :from => "deal[community_id]"
        fill_in "Url",        with: "http://www.amazon.com"
        fill_in "Description",     with: "This is the best Deal!"

        expect { click_button submit }.to change(Deal, :count).by(1)
      end
    end
  end

私は database_cleaner gem を使用しており、それを構成すると、各例の後に db レコードがクリーンアップされることがわかっています。なんらかの理由で、この1つのことが私に多くの問題を引き起こしています.

spec_helper.rb

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  # 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}

  # Checks for pending migrations before tests are run.
  # If you are not using ActiveRecord, you can remove this line.
  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

  RSpec.configure do |config|
    # ## Mock Framework
    #
    # 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

# Devise support
#config.include Devise::TestHelpers, :type => :controller

config.before(:suite) do
  DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
  DatabaseCleaner.start
  Rails.logger.debug "DB Clean start"
end

config.after(:each) do
  DatabaseCleaner.clean
  Rails.logger.debug "DB Clean clean"
end

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/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.

#for database_cleaner
config.use_transactional_fixtures = false

# 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"
# Include the Capybara DSL so that specs in spec/requests still work.
config.include Capybara::DSL
# Disable the old-style object.should syntax.
config.expect_with :rspec do |c|
  c.syntax = :expect
end


 end
end

Spork.each_run do
  # This code will be run each time you run your specs.

end

factory.rb

FactoryGirl.define do
  factory :user do
    sequence(:username)  { |n| "Person_#{n}" }
    sequence(:email) { |n| "person_#{n}@example.com" }
    password "password"
    password_confirmation "password"
  end

  factory :community do
    sequence(:name)  { |n| "Community#{n}" }
    sequence(:title) { |n| "Community #{n} Title" }
    sequence(:description) { |n| "a great community #{n}" }
    user
  end

  factory :deal do
    sequence(:title) { |n| "Great Deal #{n}" }
    url "http://www.amazon.com"
    sequence(:description) { |n| "deal #{n} rocks so click it" }
    user
    community
  end
end
4

2 に答える 2

1

問題は、「Community7」を渡すことです

let(:community) { FactoryGirl.create :community, name: "Community7", title: "Community 7", description: "A great community", user: user }

「コミュニティ」という単語も factory: community: を介して追加されます。

factory :community do
    sequence(:name)  { |n| "Community#{n}" }
    sequence(:title) { |n| "Community #{n} Title" }
    ...
end

その結果、「CommunityCommunity7」とそれに対応するエラーが発生します。

Unable to find option "Community7"

工場を次のように調整します

factory :community do
    sequence(:name)  { |n| "#{n}" }
    sequence(:title) { |n| "#{n} Title" }
    ...
end
于 2013-10-18T08:55:00.993 に答える