2011-05-09 104 views
4

我有一個模型與工廠女孩

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

,並在鐵軌控制檯我沒有問題,做

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/[email protected]/gems/activemodel-3.0.5/lib/active_model/attribute_methods.rb:364:in `method_missing' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/activerecord-3.0.5/lib/active_record/attribute_methods.rb:46:in `method_missing' 
    # /Users/bradphelan/.rvm/gems/[email protected]/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/proxy/build.rb:13:in `set' 
    # /Users/bradphelan/.rvm/gems/[email protected]/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/attribute/static.rb:12:in `add_to' 
    # /Users/bradphelan/.rvm/gems/[email protected]/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:93:in `block in run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:91:in `each' 
    # /Users/bradphelan/.rvm/gems/[email protected]/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:91:in `run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/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/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `instance_eval' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `run_in' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `block in run_all' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `each' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `run_all' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:110:in `run_hook' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `block in eval_before_eachs' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `each' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `eval_before_eachs' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:144:in `run_before_each' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:48:in `block (2 levels) in run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:106:in `with_around_hooks' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:46:in `block in run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:99:in `block in with_pending_capture' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `catch' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `with_pending_capture' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:45:in `run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:262:in `block in run_examples' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `map' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `run_examples' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:232:in `run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block (2 levels) in run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `map' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block in run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/reporter.rb:12:in `report' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:24:in `run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run' 
    # /Users/bradphelan/.rvm/gems/[email protected]/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun' 

我完全就可以難住了。任何想法

布拉德

回答

7

你運行rake db:test:prepare?它在我看來像你的Post模型有published_date,但你的工廠說它不存在。不運行這個rake任務將是會導致這種情況的主要事情。

+0

這就是問題所在。我不知道爲什麼我忘了這麼做????????感謝你的回答 – bradgonesurfing 2011-05-09 22:21:08