Rick Tessner
2007-Jun-09 20:05 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
Hi all, I feel like I''m missing something really easy and I''m just not seeing it. I''m using the restful_authentication plugin and have a User model. Uesr has_many :things and Thing belongs_to :user. That''s it. I did a "script/generate rspec_scaffold thing" to generate all the necessary bits. The "rake db:migrate" to create the db. At this point, a "rake spec" works just fine. All specs pass. I add in the "include AuthenticatedSystem" in the ThingsController and add a line to the "before" section in each of the describe stanzas that reads: controller.should_receive(:login_required).with(no_args).once.and_return(true) All tests pass just fine at this point as well. It''s this next part where I get really confused about how the spec should be written. What I change in the controller itself (and at this point, all methods are the default generated by the scaffold) is to add scoping of the current user to the finds: For example, the "index" method becomes (to find all things for the current user) @things = current_user.things.find(:all) rather than @things = Thing.find(:all) I run "rake spec" and of course it fails. So, I try to mock/stub the current_user and add a stub for the things method on it and it''s at this point that I feel like I''m missing something fundamental since I get many many failures of various types. LocalJumpError in ''ThingsController handling GET /things should render index template'' no block given is the major error. If I remove the "find(:all)" a number of these LocalJumpErrors disappear. I am interested in how to resolve the LocalJumpError issue tho since I''m sure it will be a use-case that crops up in my application. The basic issue I''m struggling with is how does define mocks/stubs for a controller method that does something of the form: method_a.method_b.find Pasted code/spec/errors here * things_controller http://pastie.caboo.se/69136 * things_controller_spec http://pastie.caboo.se/69137 * Errors w/ find(:all) http://pastie.caboo.se/69138 -- Rick rick.tessner at gmail.com
Joe Van Dyk
2007-Jun-09 21:34 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
Your pasties have wrapped lines in them for some reason, which makes matching up the errors to the lines of code difficult. On 6/9/07, Rick Tessner <rick.tessner at gmail.com> wrote:> Hi all, > > I feel like I''m missing something really easy and I''m just not seeing > it. > > I''m using the restful_authentication plugin and have a User model. Uesr > has_many :things and Thing belongs_to :user. > > That''s it. > > I did a "script/generate rspec_scaffold thing" to generate all the > necessary bits. The "rake db:migrate" to create the db. > > At this point, a "rake spec" works just fine. All specs pass. > > I add in the "include AuthenticatedSystem" in the ThingsController and > add a line to the "before" section in each of the describe stanzas that > reads: > > controller.should_receive(:login_required).with(no_args).once.and_return(true) > > All tests pass just fine at this point as well. > > It''s this next part where I get really confused about how the spec > should be written. > > What I change in the controller itself (and at this point, all methods > are the default generated by the scaffold) is to add scoping of the > current user to the finds: > > For example, the "index" method becomes (to find all things for the > current user) > > @things = current_user.things.find(:all) > > rather than > > @things = Thing.find(:all) > > I run "rake spec" and of course it fails. So, I try to mock/stub the > current_user and add a stub for the things method on it and it''s at this > point that I feel like I''m missing something fundamental since I get > many many failures of various types. > > LocalJumpError in ''ThingsController handling GET /things should > render index template'' > no block given > > is the major error. If I remove the "find(:all)" a number of these > LocalJumpErrors disappear. I am interested in how to resolve the > LocalJumpError issue tho since I''m sure it will be a use-case that crops > up in my application. > > The basic issue I''m struggling with is how does define mocks/stubs for a > controller method that does something of the form: > > method_a.method_b.find > > Pasted code/spec/errors here > > * things_controller http://pastie.caboo.se/69136 > * things_controller_spec http://pastie.caboo.se/69137 > * Errors w/ find(:all) http://pastie.caboo.se/69138 > > -- > Rick > rick.tessner at gmail.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Rick Tessner
2007-Jun-09 22:43 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
On Sat, 2007-09-06 at 14:34 -0700, Joe Van Dyk wrote:> Your pasties have wrapped lines in them for some reason, which makes > matching up the errors to the lines of code difficult.Dang. Sorry about that. Looks like it was the things_controller_spec was messed up for some reason. Re-pastied that. The three pasties are then: things_controller http://pastie.caboo.se/69136 things_controller_spec http://pastie.caboo.se/69166 Errors w/ find(:all) http://pastie.caboo.se/69138 -- Rick Tessner <ricktessner at canada.com>
Rick Tessner
2007-Jun-09 22:58 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
On Sat, 2007-09-06 at 14:34 -0700, Joe Van Dyk wrote:> Your pasties have wrapped lines in them for some reason, which makes > matching up the errors to the lines of code difficult.Dang. Sorry about that. Looks like it was the things_controller_spec was messed up for some reason. Re-pastied that. The three pasties are then: * things_controller http://pastie.caboo.se/69136 * things_controller_spec http://pastie.caboo.se/69166 * Errors w/ find(:all) http://pastie.caboo.se/69138 -- Rick rick.tessner at gmail.com
Joe Van Dyk
2007-Jun-10 19:18 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
On 6/9/07, Rick Tessner <ricktessner at canada.com> wrote:> On Sat, 2007-09-06 at 14:34 -0700, Joe Van Dyk wrote: > > Your pasties have wrapped lines in them for some reason, which makes > > matching up the errors to the lines of code difficult. > > Dang. Sorry about that. Looks like it was the things_controller_spec > was messed up for some reason. Re-pastied that. > > The three pasties are then: > > things_controller http://pastie.caboo.se/69136 > things_controller_spec http://pastie.caboo.se/69166 > Errors w/ find(:all) http://pastie.caboo.se/69138I don''t have the solution and I''ve ran into this in the past. But at least I can explain what is happening. :-) @thing = mock_model(Thing) Thing.stub!(:find).and_return([@thing]) @current_user = mock_model(User, :things => [@thing]) controller.should_receive(:login_required).with(no_args).once.and_return(true) controller.should_receive(:current_user).with(no_args).and_return(@current_user) So, when you do: current_user.things.find You are calling the find method on an array that contains one @thing object. The find you are calling is Enumerable#find (the plain old Ruby version, not the ActiveRecord::Base::find version). Enumerable#find expects a block. i.e. ["hello, "world"].find { |e| e.include?(''el'') } would return "hello" However, in the Rails code, when you call current_user.things.find, You are calling Thing::find with a scoped find for only the Things that belong to the current user. That''s an entirely different find method. Hope that makes sense. I don''t know what the solution is. Joe Van Dyk
s.ross
2007-Jun-10 22:08 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
Try pulling do_get outside the block and see if that helps... Just a thought. On Jun 10, 2007, at 12:18 PM, Joe Van Dyk wrote:> On 6/9/07, Rick Tessner <ricktessner at canada.com> wrote: >> On Sat, 2007-09-06 at 14:34 -0700, Joe Van Dyk wrote: >>> Your pasties have wrapped lines in them for some reason, which makes >>> matching up the errors to the lines of code difficult. >> >> Dang. Sorry about that. Looks like it was the >> things_controller_spec >> was messed up for some reason. Re-pastied that. >> >> The three pasties are then: >> >> things_controller http://pastie.caboo.se/69136 >> things_controller_spec http://pastie.caboo.se/69166 >> Errors w/ find(:all) http://pastie.caboo.se/69138 > > I don''t have the solution and I''ve ran into this in the past. But at > least I can explain what is happening. :-) > > @thing = mock_model(Thing) > Thing.stub!(:find).and_return([@thing]) > > @current_user = mock_model(User, :things => [@thing]) > controller.should_receive(:login_required).with > (no_args).once.and_return(true) > controller.should_receive(:current_user).with > (no_args).and_return(@current_user) > > So, when you do: > current_user.things.find > > You are calling the find method on an array that contains one @thing > object. The find you are calling is Enumerable#find (the plain old > Ruby version, not the ActiveRecord::Base::find version). > Enumerable#find expects a block. > > i.e. > ["hello, "world"].find { |e| e.include?(''el'') } > would return "hello" > > However, in the Rails code, when you call current_user.things.find, > You are calling Thing::find with a scoped find for only the Things > that belong to the current user. That''s an entirely different find > method. > > Hope that makes sense. I don''t know what the solution is. > > Joe Van Dyk > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersSteve Ross sross at calicowebdev.com http://www.calicowebdev.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070610/e27cbc70/attachment-0001.html
Rick Tessner
2007-Jun-11 00:16 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
On Sun, 2007-10-06 at 15:08 -0700, s.ross wrote:> Try pulling do_get outside the block and see if that helps...Nope, no difference there. Joe''s explanation of Enumerable#find vs. the find method that rails uses makes sense and certainly explains the error. Not sure yet how to work around this one ... -- Rick rick.tessner at gmail.com
François Beausoleil
2007-Jun-12 19:36 UTC
[rspec-users] authentication, controller specs. I think I''m missing something simple ....
Hi, 2007/6/10, Rick Tessner <rick.tessner at gmail.com>:> Nope, no difference there. Joe''s explanation of Enumerable#find vs. the > find method that rails uses makes sense and certainly explains the > error. > > Not sure yet how to work around this one ...You have to mock #find on the Array instance itself. things_proxy = [@thing] things_proxy.stub!(:find).and_return(things_proxy) @current_user = mock_model(User, :things => things_proxy) Hope that helps ! -- Fran?ois Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/
I''m refactoring some of my code into a plugin and while it passes the base specs (as though it were part of the Rails app), I''d prefer that the plugin''s specs live in the vendor/plugins/myplugin directory. Here''s where I''m getting stuck. The plugin is MVC so instead of touching one part of Rails, it relies on several (model, view, controller) to work. That means to spec it requires rspec_on_rails. So, my questions are: 1. Is anyone doing this? 2. Was it worth the effort or is Test::Unit easier for this kind of thing? I''d be happy if I could even get a model spec to pass, but I keep getting version mismatches between spec and rspec_on_rails even though I''ve done fresh gem install and plugin installs. Any thoughts appreciated. I''d far prefer to use only rSpec on this project. Thanks
On 6/12/07, s.ross <cwdinfo at gmail.com> wrote:> I''m refactoring some of my code into a plugin and while it passes the > base specs (as though it were part of the Rails app), I''d prefer that > the plugin''s specs live in the vendor/plugins/myplugin directory. > > Here''s where I''m getting stuck. The plugin is MVC so instead of > touching one part of Rails, it relies on several (model, view, > controller) to work. That means to spec it requires rspec_on_rails. > So, my questions are: > > 1. Is anyone doing this?Yes, at least the M with plugins playing with Models. I just created a spec folder, a spec_helper.rb inside of it and some Fixtured Models, Database schema. I used that approach to pluginize some code of one application. Specs inside the plugin will be independent of models located in your application, so: app/models/user.rb will not clash with #{my_plugin}/spec/fixtures/models/user.rb in that way, I isolate the plugin functionality and his behavior. This is the pastie for my spec_helper inside plugin/spec folder: http://pastie.caboo.se/69984> 2. Was it worth the effort or is Test::Unit easier for this kind of > thing? > > I''d be happy if I could even get a model spec to pass, but I keep > getting version mismatches between spec and rspec_on_rails even > though I''ve done fresh gem install and plugin installs. >I don''t know about that, I''m stuck at 0.9.4 gem + plugins until migrate some of these applications.> Any thoughts appreciated. I''d far prefer to use only rSpec on this > project.-- Luis Lavena Multimedia systems - Leaders are made, they are not born. They are made by hard effort, which is the price which all of us must pay to achieve any goal that is worthwhile. Vince Lombardi
On 6/12/07, s.ross <cwdinfo at gmail.com> wrote:> I''m refactoring some of my code into a plugin and while it passes the > base specs (as though it were part of the Rails app), I''d prefer that > the plugin''s specs live in the vendor/plugins/myplugin directory. > > Here''s where I''m getting stuck. The plugin is MVC so instead of > touching one part of Rails, it relies on several (model, view, > controller) to work. That means to spec it requires rspec_on_rails. > So, my questions are: > > 1. Is anyone doing this? > 2. Was it worth the effort or is Test::Unit easier for this kind of > thing? > > I''d be happy if I could even get a model spec to pass, but I keep > getting version mismatches between spec and rspec_on_rails even > though I''ve done fresh gem install and plugin installs. > > Any thoughts appreciated. I''d far prefer to use only rSpec on this > project. > > Thanks > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >You might want to check out the rspec_plugin generator - http://agilewebdevelopment.com/plugins/rspec_plugin Pat
Thanks, this is a great starting point. Steve On Jun 12, 2007, at 6:23 PM, Luis Lavena wrote:> On 6/12/07, s.ross <cwdinfo at gmail.com> wrote: >> I''m refactoring some of my code into a plugin and while it passes the >> base specs (as though it were part of the Rails app), I''d prefer that >> the plugin''s specs live in the vendor/plugins/myplugin directory. >> >> Here''s where I''m getting stuck. The plugin is MVC so instead of >> touching one part of Rails, it relies on several (model, view, >> controller) to work. That means to spec it requires rspec_on_rails. >> So, my questions are: >> >> 1. Is anyone doing this? > > Yes, at least the M with plugins playing with Models. > > I just created a spec folder, a spec_helper.rb inside of it and some > Fixtured Models, Database schema. > > I used that approach to pluginize some code of one application. > > Specs inside the plugin will be independent of models located in your > application, so: > > app/models/user.rb will not clash with #{my_plugin}/spec/fixtures/ > models/user.rb > > in that way, I isolate the plugin functionality and his behavior. > > This is the pastie for my spec_helper inside plugin/spec folder: > > http://pastie.caboo.se/69984 > > >> 2. Was it worth the effort or is Test::Unit easier for this kind of >> thing? >> >> I''d be happy if I could even get a model spec to pass, but I keep >> getting version mismatches between spec and rspec_on_rails even >> though I''ve done fresh gem install and plugin installs. >> > > I don''t know about that, I''m stuck at 0.9.4 gem + plugins until > migrate some of these applications. > >> Any thoughts appreciated. I''d far prefer to use only rSpec on this >> project. > > -- > Luis Lavena > Multimedia systems > - > Leaders are made, they are not born. They are made by hard effort, > which is the price which all of us must pay to achieve any goal that > is worthwhile. > Vince Lombardi > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersSteve Ross sross at calicowebdev.com http://www.calicowebdev.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070612/4a503c1f/attachment.html