<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Q. Why does code which defines a context work in the <tt>controller_spec</tt>
file and not in a 'require'd helper file?<br>
<br>
A. Because spec uses the filename to tell it what type of context
(:controller, :model) it is creating.<br>
<br>
Fix: use the (undocumented?) second parameter to <tt>#context(), </tt><b><tt>:context_type
=&gt; :controller.<br>
<br>
</tt></b>Quite how this works, I do not know; the rdocs don't mention
it and according to the source context doesn't take a second argument
(except a block).&nbsp; My Ruby-fu isn't up to explaining it; if anyone else
would like to do so that would be nice.<b><br>
</b><br>
Hope this helps someone else save a half hour or so.<br>
<br>
Rgds,<br>
&nbsp; Jerry<br>
<br>
Further details:<br>
<br>
If I define a method which dynamically constructs a context in my
controller_spec.rb file it works fine:<br>
<br>
<tt>def restful_edit_specs(resource)<br>
&nbsp; context "GET /#{resource}/:id;edit (:edit)" do&nbsp; <br>
&nbsp;&nbsp;&nbsp; controller_name resource<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; resource = resource.to_s<br>
&nbsp;&nbsp;&nbsp; sym = resource.singularize<br>
&nbsp;&nbsp;&nbsp; klass = resource.classify<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; setup do<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @mock = mock_model sym # based on
<a class="moz-txt-link-freetext" href="http://metaclass.org/2006/12/22/making-a-mockery-of-activerecord">http://metaclass.org/2006/12/22/making-a-mockery-of-activerecord</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @model = Object.const_get(klass)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @model.should_receive(:find).with(@mock.id).and_return(@mock)<br>
&nbsp;&nbsp;&nbsp; end<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; def do_get() get :edit, :id =&gt; @mock.id end<br>
<br>
&nbsp;&nbsp;&nbsp; specify "should be successful" do<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; response.should_be_success<br>
&nbsp;&nbsp;&nbsp; end<br>
&nbsp;&nbsp;&nbsp; specify "should render edit.rhtml" do<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; controller.should_render :edit<br>
&nbsp;&nbsp;&nbsp; end<br>
&nbsp;&nbsp;&nbsp; specify "should find the #{klass} requested" do<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @model.should_receive(:find).and_return(@mock)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>
&nbsp;&nbsp;&nbsp; end<br>
&nbsp;&nbsp;&nbsp; specify "should assign the found #{klass} for the view" do<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assigns(sym).should_equal @mock<br>
&nbsp;&nbsp;&nbsp; end<br>
&nbsp; end<br>
end<br>
<br>
restful_edit_specs(:assessments)<br>
<br>
</tt>This all works fine when defined in the xx_controller_spec.rb file.<br>
<br>
But if I move the exact same code to a helper file and require it, spec
gets confused:<br>
<br>
<tt>.../rspec-0.7.5.1/lib/spec/expectations/sugar.rb:13:in `call':
undefined method `controller_name' for
#&lt;Spec::Runner::ContextEvalModule:0xb70496a8&gt; (NoMethodError)<br>
&nbsp;... etc etc...<br>
</tt><br>
Clearly, there's some magic involved which means the context does not
know what it's about (it was at this point in typing my original plea
for help that I remembered seeing the :context_type parameter in the
specs for the rspec_on_rails plugin itself).<br>
<br>
Changing the context line to<br>
<br>
<tt>&nbsp;&nbsp;&nbsp; </tt><b><tt>context "GET /#{resource}/:id;edit (:edit)", </tt><tt>:context_type
=&gt; :controller do<br>
<br>
</tt></b>allows the specs to function as ex-spec-ted.<b><tt><br>
</tt></b>
</body>
</html>