thoughtbot / factory_bot

A library for setting up Ruby objects as test data.
https://thoughtbot.com
MIT License
7.92k stars 2.6k forks source link

Have a column equal exists function name #439

Closed stamm closed 12 years ago

stamm commented 12 years ago

I have ApplicationHelper, with function "title" I need in test, for example generate title

module ApplicationHelper
  # Return a title on a per-page basis.
  def title
    base_title = "Blog"
    if @title.nil?
      base_title
    else
      "#{base_title} | #{@title}"
    end
  end
end

I have a post with column "title"

spec/factories.rb

FactoryGirl.define do
  factory :post do
    title  "Title #1"
  end
end

spec/support/utilities.rb

include ApplicationHelper

spec/requests/articles_pages_spec.rb

require 'spec_helper'

describe "Articles page" do
  subject { page }

  describe "index page" do
    let(:post) { FactoryGirl.create(:post) }
    before { visit root_path }

    it { should have_selector('h2 a', text: post.title) }
  end

end

Run test and get error

rspec spec/requests/articles_pages_spec.rb

/Users/stamm/Sites/ruby/blog-ror/app/helpers/application_helper.rb:3:in `title': wrong number of arguments (1 for 0) (ArgumentError)
    from /Users/stamm/Sites/ruby/blog-ror/spec/factories.rb:10:in `block (2 levels) in <top (required)>'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in `instance_eval'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in `factory'
    from /Users/stamm/Sites/ruby/blog-ror/spec/factories.rb:9:in `block in <top (required)>'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `run'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:7:in `define'
    from /Users/stamm/Sites/ruby/blog-ror/spec/factories.rb:1:in `<top (required)>'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `block in load'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/factory_girl-4.1.0/lib/factory_girl/reload.rb:6:in `reload'
    from /Users/stamm/Sites/ruby/blog-ror/spec/spec_helper.rb:50:in `block in <top (required)>'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/spork-0.9.2/lib/spork.rb:37:in `each_run'
    from /Users/stamm/Sites/ruby/blog-ror/spec/spec_helper.rb:48:in `<top (required)>'
    from /Users/stamm/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/stamm/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/stamm/Sites/ruby/blog-ror/spec/requests/articles_pages_spec.rb:1:in `<top (required)>'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:66:in `rescue in run'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:62:in `run'
    from /Users/stamm/.rvm/gems/ruby-1.9.2-p320/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

How to make it work?

joshuaclayton commented 12 years ago

@Stamm Could you paste your full spec/factories.rb file (or at least the first 20-30 lines)? Thanks!

stamm commented 12 years ago

@joshuaclayton This is all file. I have a repo with this problem: https://github.com/Stamm/blog-ror

joshuaclayton commented 12 years ago

@Stamm after looking at the project, it looks like you've gotten it figured out! This did it: https://github.com/Stamm/blog-ror/commit/60a0b83a57b7658cb2bde2cd274d6bb515d907bf. When you were calling include ApplicationHelper, it was messing with setting title on your factory since ApplicationHelper was adding a title method to Object, meaning everything now responded to title.

Let me know if you're having any other issues; I'll close the issue for now. Thanks!