Hello everyone: Right now I am writing spec on modules, which are provided by my colleagues. Some of the modules actually contain action methods. I tried very hard to spec those action methods in modules. But it seems that the rspec does not allow module spec to ''get'' action like controller does. After I saw the documentation, I then used :behaviour_type=>:controller. However, it failed again. It reported an error for me. For illustration, I''d like a simple example. module MyModule def copy #an action method render :partial=>"/index", :layout=>false end end describe MyModule, :behaviour_type=>:controller do it "should render partial index" do get ''copy'' #test code not provided yet, just want to get the action end end The error reported was: undefined method ''new'' for MyModule:Module. Do you guys have any idea of the error? And how should I test the action methods in modules? Cheers! -- View this message in context: http://www.nabble.com/Can-module-spec-%22behave-like%22-controller-spec--tf4358891.html#a12422548 Sent from the rspec-users mailing list archive at Nabble.com.
David Chelimsky
2007-Aug-31 08:50 UTC
[rspec-users] Can module spec "behave like" controller spec?
On 8/31/07, Shaker <flyeminent at hotmail.com> wrote:> > Hello everyone: > Right now I am writing spec on modules, which are provided by my > colleagues. Some of the modules actually contain action methods. I tried > very hard to spec those action methods in modules. But it seems that the > rspec does not allow module spec to ''get'' action like controller does. After > I saw the documentation, I then used :behaviour_type=>:controller. However, > it failed again. It reported an error for me. For illustration, I''d like a > simple example. > module MyModule > def copy #an action method > render :partial=>"/index", :layout=>false > end > end > > describe MyModule, :behaviour_type=>:controller do > it "should render partial index" do > get ''copy'' #test code not provided yet, just want to get the action > end > end > The error reported was: undefined method ''new'' for MyModule:Module. Do you > guys have any idea of the error? And how should I test the action methods in > modules?The ControllerBehaviour is trying to instantiate MyModule. What you''d need to do is something like this: class ControllerThatUsesMyModule include MyModule end describe ControllerThatUsesMyModule, :behaviour_type=>:controller do it "should render partial index" do get ''copy'' #test code not provided yet, just want to get the action end end I haven''t tried it, but it seems like it should work. Give it a whirl and report back please. Cheers, David> Cheers! > > > -- > View this message in context: http://www.nabble.com/Can-module-spec-%22behave-like%22-controller-spec--tf4358891.html#a12422548 > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2007-Aug-31 09:00 UTC
[rspec-users] Can module spec "behave like" controller spec?
On 8/31/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 8/31/07, Shaker <flyeminent at hotmail.com> wrote: > > > > Hello everyone: > > Right now I am writing spec on modules, which are provided by my > > colleagues. Some of the modules actually contain action methods. I tried > > very hard to spec those action methods in modules. But it seems that the > > rspec does not allow module spec to ''get'' action like controller does. After > > I saw the documentation, I then used :behaviour_type=>:controller. However, > > it failed again. It reported an error for me. For illustration, I''d like a > > simple example. > > module MyModule > > def copy #an action method > > render :partial=>"/index", :layout=>false > > end > > end > > > > describe MyModule, :behaviour_type=>:controller do > > it "should render partial index" do > > get ''copy'' #test code not provided yet, just want to get the action > > end > > end > > The error reported was: undefined method ''new'' for MyModule:Module. Do you > > guys have any idea of the error? And how should I test the action methods in > > modules? > > The ControllerBehaviour is trying to instantiate MyModule. What you''d > need to do is something like this: > > class ControllerThatUsesMyModule > include MyModule > end > > describe ControllerThatUsesMyModule, :behaviour_type=>:controller do > it "should render partial index" do > get ''copy'' #test code not provided yet, just want to get the action > end > end > > I haven''t tried it, but it seems like it should work. Give it a whirl > and report back please.Of course, the subject of this thread should smack us all in the face and point in a different direction: describe "any controller", :shared => true do it "should render partial index" do get ''copy'' #test code not provided yet, just want to get the action end end # an actual controller - not just for specs class FooController include AnyController end # in spec/controllers/foo_controller_spec.rb describe FooController do it_should_behave_like "any controller" ... #more stuff specific to FooController end> > Cheers, > David > > > Cheers! > > > > > > -- > > View this message in context: http://www.nabble.com/Can-module-spec-%22behave-like%22-controller-spec--tf4358891.html#a12422548 > > Sent from the rspec-users mailing list archive at Nabble.com. > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > >