工厂女孩的奇怪问题

时间:2011-05-09 21:20:02

标签: ruby ruby-on-rails-3 activerecord rspec factory-bot

我有一个模特

# == Schema Information
#
# Table name: posts
#
#  id             :integer         not null, primary key
#  name           :string(255)
#  title          :string(255)
#  content        :text
#  created_at     :datetime
#  updated_at     :datetime
#  abstract       :text
#  category_id    :integer
#  finalversion   :boolean         default(FALSE)
#  published_date :date
#

class Post < ActiveRecord::Base
  has_many :tags
  belongs_to :category

  validates :published_date, :presence => true

  default_scope :order => 'created_at DESC'

  accepts_nested_attributes_for :tags, :allow_destroy => :true,
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }

  def prev_post
    self.class.first(:conditions => ["id < ?", id], :order => "id desc")
  end


  def next_post
    self.class.first(:conditions => ["id > ?", id], :order => "id asc")
  end

  def seo_title
    title.gsub(/\s+/,'_')
  end

end

和工厂

FactoryGirl.define do
  factory :post do
    published_date Date.today
    association :category, :factory => :category
    title Forgery::LoremIpsum.words
    name Forgery::LoremIpsum.word
    content Forgery::LoremIpsum.words(100, :random => 250)
    abstract Forgery::LoremIpsum.words(100, :random => 50)
    finalversion true
  end
end

并且在rails控制台中我没有问题

FactoryGirl.create :post

获取有效对象并能够访问* published_date *属性。但是在我的规范中

require 'spec_helper'


(1..5).map do |i|
  title        = "a title\t#{i}"
  escape_title = "a_title_#{i}"
  perma_link = "/posts/#{i}/title/#{escape_title}"

  describe "A post with title '#{title}'" do

    before do
      @post = FactoryGirl.create :post, :id=>i, :title => title
      visit '/posts'
    end

    it "should appear in all links with permalink #{perma_link}" do
      within "section.post_#{@post.id}" do
        page.should have_xpath(%Q%.//h1/a[@href="#{perma_link}"]%)
        within "div.teaser" do
          page.should have_xpath(%Q%.//a[@href="#{perma_link}"]%)
        end
      end
    end
  end
end

我收到了回溯错误

  5) A post with title 'a title 5' should appear in all links with permalink /posts/5/title/a_title_5
     Failure/Error: @post = FactoryGirl.create :post, :id=>i, :title => title
     NoMethodError:
       undefined method `published_date=' for #<Post:0x000001047266b8>
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/activemodel-3.0.5/lib/active_model/attribute_methods.rb:364:in `method_missing'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/activerecord-3.0.5/lib/active_record/attribute_methods.rb:46:in `method_missing'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/proxy/build.rb:13:in `set'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/attribute/static.rb:12:in `add_to'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:93:in `block in run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:91:in `each'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:91:in `run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/syntax/methods.rb:54:in `create'
     # ./spec/integration/posts_permalinks_spec.rb:12:in `block (3 levels) in <top (required)>'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `instance_eval'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `run_in'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `block in run_all'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `each'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `run_all'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:110:in `run_hook'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `block in eval_before_eachs'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `each'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `eval_before_eachs'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:144:in `run_before_each'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:48:in `block (2 levels) in run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:106:in `with_around_hooks'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:46:in `block in run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:99:in `block in with_pending_capture'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `catch'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `with_pending_capture'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:45:in `run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:262:in `block in run_examples'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `map'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `run_examples'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:232:in `run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block (2 levels) in run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `map'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block in run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/reporter.rb:12:in `report'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:24:in `run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
     # /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180@ingd/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'

我完全被它难住了。任何想法

布拉德

1 个答案:

答案 0 :(得分:7)

你运行rake db:test:prepare了吗?在我看来,您的Post型号有published_date,但您的工厂说它不存在。不运行此rake任务将是导致此问题的主要因素。