Re: [rspec] Testing a custom form builder with rspec

396 views
Skip to first unread message

David Chelimsky

unread,
Dec 3, 2012, 6:03:30 AM12/3/12
to rs...@googlegroups.com
On Mon, Dec 3, 2012 at 12:34 AM, macarthy <justin.m...@gmail.com> wrote:
> Does anyone have an example of testing a custom form builder with rspec ?
>
> in /spec/helpers/my_form_builder_spec.rb
>
> require 'spec_helper'
>
> describe MyFormBuilder do
>
> before (:each) do
>
> @object = mock_model(Company)
>
> @object.stub!(:longname).and_return("Company")
>
> @object.stub!(:phone).and_return("+(66) 000 00001")
>
> @builder = StandardFormBuilder.new(:company, @object, self, {}, nil) end
>
> end
>
>
>
> I'm getting the following error
>
> wrong argument type Class (expected Module)

Which line in what file is this coming from? Please post the backtrace
so we can see more detail.

>
> I think it is try to include RSpec::Rails::HelperExampleGroup
>
> Any ideas?
>
>
> Thanks J
>
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To post to this group, send email to rs...@googlegroups.com.
> To unsubscribe from this group, send email to
> rspec+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rspec/-/rwqPfaLVM-0J.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

macarthy

unread,
Dec 4, 2012, 11:25:35 PM12/4/12
to rs...@googlegroups.com



Here is the backtrace , I'm using :type => :helper since my formbuilder is in the li directory 

Thanks 

----- output ----

StandardFormBuilder
  standard fields
    should build input fields (FAILED - 1)

Failures:

  1) StandardFormBuilder standard fields should build input fields
     Failure/Error: Unable to find matching line from backtrace
     TypeError:
       wrong argument type Class (expected Module)
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `include'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `block in add_template_helper'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `module_eval'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `add_template_helper'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:98:in `block in helper'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:97:in `each'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:97:in `helper'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/action_view/test_case.rb:85:in `include_helper_modules!'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/action_view/test_case.rb:78:in `new'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:359:in `run'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:34:in `report'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
     # /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

Finished in 0.00117 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/lib/standard_form_builder_spec.rb:36 # StandardFormBuilder standard fields should build input fields
/Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `include': wrong argument type Class (expected Module) (TypeError)
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `block in add_template_helper'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `module_eval'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:123:in `add_template_helper'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:98:in `block in helper'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:97:in `each'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/abstract_controller/helpers.rb:97:in `helper'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/action_view/test_case.rb:85:in `include_helper_modules!'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.0.13/lib/action_view/test_case.rb:78:in `new'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:366:in `run'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `map'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block in run'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:34:in `report'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:25:in `run'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
from /Users/macarthy/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

Myron Marston

unread,
Dec 6, 2012, 3:46:39 PM12/6/12
to rs...@googlegroups.com
Looking at your backtrace, the culprit is here:


Rails is trying to include your class as a helper module but it is not a module.

For validation, compare the error you're getting to what you get in IRB with code like: 

Class.new { include Class.new }

Anyhow, it would take me a while to dig through the entire backtrace to see exactly how it is getting there, but I suspect that rails' support for testing helpers assumes that helpers are modules, and tries to mix them in to a testable object, the same way rails does to mix helper modules into a template context.  rspec-rails generally wraps what rails provides, so when you tag your example group with `:type => :helper` it is triggering this.

I recommend trying to not tag your example group with `:type => :helper`.  After all, this isn't a helper module, right?

HTH,
Myron
Reply all
Reply to author
Forward
0 new messages