S
S
Stamm2012-09-25 19:16:16
Ruby on Rails
Stamm, 2012-09-25 19:16:16

FactoryGirl: function name in helper matches column name?

There is FactoryGirl and rspec.
The Post model has a title column
. There is also a title function in the ApplicationHelper. I need this function inside other tests.

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

spec/factories.rb
FactoryGirl.define do<br>
  factory :post do<br>
    title  "Title #1"<br>
  end<br>
end<br>

spec/support/utilities.rb
include ApplicationHelper<br>
spec/requests/articles_pages_spec.rb
require 'spec_helper'<br><br>
describe "Articles page" do<br>
  subject { page }<br><br>
  describe "index page" do<br>
    let(:post) { FactoryGirl.create(:post) }<br>
    before { visit root_path }<br><br>
    it { should have_selector('h2 a', text: post.title) }<br>
  end<br><br>
end<br>

We run tests and get an 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 &lt;top (required)&gt;'
  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 &lt;top (required)&gt;'
  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 `&lt;top (required)&gt;'
  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 &lt;top (required)&gt;'
  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 `&lt;top (required)&gt;'
  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 `&lt;top (required)&gt;'
  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?
Tried like this:
FactoryGirl.define do<br>
  factory :post do |u|<br>
    u.title  "Title #1"<br>
  end<br>
end<br>

Same error

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Taraev, 2012-09-25
@ataraev

The simplest solution is to rename helper, it will definitely help

S
sl_bug, 2012-09-25
@sl_bug

do not use
spec/support/utilities.rb
include ApplicationHelper

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question