工厂女孩似乎创造了不止一个对象

时间:2013-06-23 10:21:44

标签: ruby-on-rails rspec capybara factory-bot

您好我正在使用工厂女孩来测试我的项目,但我不断遇到的一件事是工厂女孩不断创建多个对象,例如我有以下测试: -

 require 'spec_helper'

 feature 'Category' do


scenario "creation" do
    @category = FactoryGirl.create(:category)
    visit new_admin_category_path
    fill_in "category_category_name", :with => "trousers"
    page.select('Foo', :from => "category_products_products")
    click_button('Save Category')
end

scenario "editing" do
    @category = FactoryGirl.create(:category, :products_attributes => [FactoryGirl.attributes_for(:product, :name => "Barz")])
    visit edit_admin_category_path(@category.slug)
    fill_in "category_category_name", :with => "Baz"
    page.select('Barz', :from => "category_products_products")
    click_button("Save Category")
    page.driver.submit '/admin/products/#{@category.slug}', :category_name => @category.category_name, :products_attributes => @category.products_attributes
end

scenario "denied entry when not logged in" do
    visit admin_index_path
    page.should  have_content("Please log in")
    visit new_session_path
end
end

我的工厂如下: -     要求'factory_girl'

FactoryGirl.define do
  factory :category do
  category_name "Trousers"
  slug "trousers"
  department
  products_attributes { [ FactoryGirl.attributes_for(:product) ]}
end

factory :user do
 username "Foo"
 password "Bar"
end

factory :product do
  name "Foo"
  price 9999
  description "I am a foobar"
  slug "foo"
  category
  department
end

factory :department do
  department_name "Mens"
  slug "mens"
  products_attributes { [ FactoryGirl.attributes_for(:product) ]}
end
end

我收到以下错误: -

2) Category creation
Failure/Error: page.select('Foo', :from => "category_products_products")
 Capybara::Ambiguous:
   Ambiguous match, found 2 elements matching option "Foo"
 # ./spec/features/category_spec.rb:12:in `block (2 levels) in <top (required)>'

我的代码中可能出错了什么?

这是我的spec_helper: -

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# require 'rspec/autorun'
require 'capybara/rspec'
require 'database_cleaner'
require 'rack/test'

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

RSpec.configure do |config|
  config.include Rack::Test::Methods
  config.infer_base_class_for_anonymous_controllers = true
  config.order = "random"
  config.use_transactional_fixtures = false

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

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

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

 config.include Capybara::DSL

end

生成的HTML: -

    <form class="form-horizontal">
      <div class="control-group">
       <label class="control-label">
         <label for="category_category_name">Category name</label>
     </label>
    <div class="controls">
      <input id="category_category_name" name="category[category_name]" size="30"  type="text" />
    </div>
        </div>
    <div class="control-group">
    <label class="control-label">
      <label for="category_products_products">Products</label>
    </label>
    <div class="controls">
      <select>
    <select id="category_products_products" name="category[products][products]"><option value="56">Foo</option>
<option value="57">Foo</option></select>
    </select>
    </div>
    <div class"control-group">
    <div class="controls">
    <button class="btn">
     <input name="commit" type="submit" value="Save Category" />
    </button>
      </div>
      </div>
    </div>
   </form>

类别模型: -

class Category < ActiveRecord::Base
    extend FriendlyId 
    friendly_id :category_name, use: :slugged
  attr_accessible :category_name, :products_attributes, :slug

  has_many :products
  belongs_to :department

  validates_presence_of :category_name
  accepts_nested_attributes_for :products

end

查看代码: -

<%= form_for :category do  |f| %>
    <div class="category-form-new">
        <form class="form-horizontal">
            <div class="control-group">
                <label class="control-label">
                    <%= f.label :category_name %>
                </label>
                <div class="controls">
                    <%= f.text_field :category_name %>
                </div>
            </div>
            <div class="control-group">
                <%= f.fields_for :products do |product_form| %>
                <label class="control-label">
                    <%= product_form.label :products, "Products" %>
                </label>
                <div class="controls">
                    <select>
                        <%= product_form.select :products, options_from_collection_for_select(Product.all, :id, :name) %>
                    </select>
            <% end %>
            </div>
            <div class"control-group">
                <div class="controls">
                    <button class="btn">
                        <%= f.submit %>
                    </button>
                </div>
            </div>
    </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

我假设Capybara指的是你网页上的两个HTML元素,这与FactoryGirl无关。