Hi, rather than flooding this list with separate emails, I''ve been queueing up questions using rspec with rails. (an irc channel would be nice too :) Here ya go... - Is there a way to test named routes? - Could I get an example how to test an ajax request (link_to_remote)? would that go in view? and/or in controller spec? - Can you assert which layout template is rendered in a controller spec? - How to test application_helper.rb helpers? (creating a file spec/helpers/application_helper_spec.rb like other helper_spec files gives an error no such file to load -- ./spec/helpers/../application_helper (LoadError) - How to write a test (uh, i mean spec) for helpers that take blocks? for example: def link_block_to_remote( options = {}, html_options = {}, &block) concat(link_to_remote(capture(&block), options, html_options), block.binding) end - How can i test ActionMailer? eg ensure it renders the correct template. - Is there an equivalent to script/generate mailer ,eg rspec_mailer that generates specs? and/or do you have examples of mailer specs? - Do you have recommendations for using authentication? should i load a users.yml fixture or setup users some other way? (my controllers and views may check for current_user, logged_in?, etc) (I use restful_authentication) - this may not be rspec at all, I''m calling assert_difference (defined in /lib/authenticated_test_helper.rb) with 3 args but get an error that it expects 2. Is there another version of this method somewhere? I cant find it. If i rename the one i know and call that (eg assert_difference3) its ok. - do any of you also use topfunky_power_tools (ref peepcode testing video) But I dont know how to get my specs to find the topfunky test_helper.rb file. (btw, that file also has a assert_difference but it also takes 3 args... ) - peepcode also suggests vic_tests for integration testing. Have you used that, what do you think? - Running autotest, when a spec errors, autotest sometimes tries re- running the test 2, 3, sometimes many more times repeatedly although I dont touche the file again. Is this a known bug? That''s it for now :) thanks linoj
On 5/29/07, Jonathan Linowes <jonathan at parkerhill.com> wrote:> Hi, rather than flooding this list with separate emails, I''ve been > queueing up questions using rspec with rails. (an irc channel would > be nice too :) Here ya go...Actually, we prefer separate email. There''s too much here for any one person to answer quickly and since this will all be part of one thread it becomes difficult to find the great answer you got from so and so in a month. I''ll answer one.> - Running autotest, when a spec errors, autotest sometimes tries re- > running the test 2, 3, sometimes many more times repeatedly although > I dont touche the file again. Is this a known bug?It''s more of a conflict than a bug. Easily resolved: http://rubyforge.org/tracker/?func=detail&group_id=797&aid=11147&atid=3149 http://blog.davidchelimsky.net/articles/2007/05/29/rspec-and-autotest
On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote:> Hi, rather than flooding this list with separate emails, I''ve been > queueing up questions using rspec with rails. (an irc channel would > be nice too :) Here ya go... > > > - Is there a way to test named routes?I don''t think there is a very clean & direct way (hopefully the others on the list can correct me). The rspec rest generator generates some code to check the routes, so you could use something like that. Scott
On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote:> Hi, rather than flooding this list with separate emails, I''ve been > queueing up questions using rspec with rails. (an irc channel would > be nice too :) Here ya go... > > - Can you assert which layout template is rendered in a controller > spec? >I''ll answer this one: response.should render_template() Don''t forget to include either a GET or POST, and if you''re using stubs or mocks, watch test.log for errors. I''ve noticed that when I stub out a model object, for example, if I don''t stub out all the methods that get called before the action gets to my render, then the spec fails but without a lot of related info. test.log will usually give me a hint with a stack trace. It''s documented here: http://rspec.rubyforge.org/documentation/rails/ writing/controllers.html> > That''s it for now :) thanks > linoj > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Hi, I should have mentioned I already did that. Here''s my spec.opts file --colour --format progress --loadby mtime On May 29, 2007, at 7:17 AM, David Chelimsky wrote:> On 5/29/07, Jonathan Linowes <jonathan at parkerhill.com> wrote: >> Hi, rather than flooding this list with separate emails, I''ve been >> queueing up questions using rspec with rails. (an irc channel would >> be nice too :) Here ya go... > > Actually, we prefer separate email. There''s too much here for any one > person to answer quickly and since this will all be part of one thread > it becomes difficult to find the great answer you got from so and so > in a month. > > I''ll answer one. > >> - Running autotest, when a spec errors, autotest sometimes tries re- >> running the test 2, 3, sometimes many more times repeatedly although >> I dont touche the file again. Is this a known bug? > > It''s more of a conflict than a bug. Easily resolved: > > http://rubyforge.org/tracker/? > func=detail&group_id=797&aid=11147&atid=3149 > http://blog.davidchelimsky.net/articles/2007/05/29/rspec-and-autotest > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Hi, yes I realize I can assert the view template. I''m trying to assert the layout one. I''ve tried response.should render_template("pages") and response.should render_template("layouts/pages") and response.should render_template("/layouts/pages") and response.should render_template(''/app/views/layouts/site_pages'') On May 29, 2007, at 8:36 AM, Anthony Carlos wrote:> > On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote: > >> Hi, rather than flooding this list with separate emails, I''ve been >> queueing up questions using rspec with rails. (an irc channel would >> be nice too :) Here ya go... >> >> - Can you assert which layout template is rendered in a controller >> spec? >> > > I''ll answer this one: > > response.should render_template() > > Don''t forget to include either a GET or POST, and if you''re using > stubs or mocks, watch test.log for errors. I''ve noticed that when I > stub out a model object, for example, if I don''t stub out all the > methods that get called before the action gets to my render, then the > spec fails but without a lot of related info. test.log will usually > give me a hint with a stack trace. > > It''s documented here: http://rspec.rubyforge.org/documentation/rails/ > writing/controllers.html > >> >> That''s it for now :) thanks >> linoj >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
>Actually, we prefer separate email.ok, I''ll dole them out incrementally, with some more detail
to answer my own question here, @layout is a variable so response.layout.should == ''layouts/pages'' On May 29, 2007, at 11:57 AM, Jonathan Linowes wrote:> Hi, > > yes I realize I can assert the view template. I''m trying to assert > the layout one. > > I''ve tried > response.should render_template("pages") > and > response.should render_template("layouts/pages") > and > response.should render_template("/layouts/pages") > and > response.should render_template(''/app/views/layouts/site_pages'') > > > On May 29, 2007, at 8:36 AM, Anthony Carlos wrote: > >> >> On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote: >> >>> Hi, rather than flooding this list with separate emails, I''ve been >>> queueing up questions using rspec with rails. (an irc channel would >>> be nice too :) Here ya go... >>> >>> - Can you assert which layout template is rendered in a controller >>> spec? >>> >> >> I''ll answer this one: >> >> response.should render_template() >> >> Don''t forget to include either a GET or POST, and if you''re using >> stubs or mocks, watch test.log for errors. I''ve noticed that when I >> stub out a model object, for example, if I don''t stub out all the >> methods that get called before the action gets to my render, then the >> spec fails but without a lot of related info. test.log will usually >> give me a hint with a stack trace. >> >> It''s documented here: http://rspec.rubyforge.org/documentation/rails/ >> writing/controllers.html >> >>> >>> That''s it for now :) thanks >>> linoj >>> >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On 5/29/07, Jonathan Linowes <jonathan at parkerhill.com> wrote:> to answer my own question here, @layout is a variable so > > response.layout.should == ''layouts/pages''This is a great case for a custom matcher, if you''re interested. Something like: response.should use_layout(''pages'') Check out http://blog.davidchelimsky.net/articles/2007/02/18/custom-expectation-matchers for info on how-to.> > > > On May 29, 2007, at 11:57 AM, Jonathan Linowes wrote: > > > Hi, > > > > yes I realize I can assert the view template. I''m trying to assert > > the layout one. > > > > I''ve tried > > response.should render_template("pages") > > and > > response.should render_template("layouts/pages") > > and > > response.should render_template("/layouts/pages") > > and > > response.should render_template(''/app/views/layouts/site_pages'') > > > > > > On May 29, 2007, at 8:36 AM, Anthony Carlos wrote: > > > >> > >> On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote: > >> > >>> Hi, rather than flooding this list with separate emails, I''ve been > >>> queueing up questions using rspec with rails. (an irc channel would > >>> be nice too :) Here ya go... > >>> > >>> - Can you assert which layout template is rendered in a controller > >>> spec? > >>> > >> > >> I''ll answer this one: > >> > >> response.should render_template() > >> > >> Don''t forget to include either a GET or POST, and if you''re using > >> stubs or mocks, watch test.log for errors. I''ve noticed that when I > >> stub out a model object, for example, if I don''t stub out all the > >> methods that get called before the action gets to my render, then the > >> spec fails but without a lot of related info. test.log will usually > >> give me a hint with a stack trace. > >> > >> It''s documented here: http://rspec.rubyforge.org/documentation/rails/ > >> writing/controllers.html > >> > >>> > >>> That''s it for now :) thanks > >>> linoj > >>> > >>> > >>> _______________________________________________ > >>> rspec-users mailing list > >>> rspec-users at rubyforge.org > >>> http://rubyforge.org/mailman/listinfo/rspec-users > >> > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-users at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
cool! it works!! I put it all in spec_helper.rb Is there a better place to put it? your post implies the class and the method should be in separate files? (I dont know what is meant by "#included in specify blocks") http://blog.davidchelimsky.net/articles/2007/02/18/expectation-matchers # in spec_helper.rb class UseLayout def initialize(expected) @expected = ''layouts/'' + expected end def matches?(controller) @actual = controller.layout #@actual.equal?(@expected) @actual == @expected end def failure_message return "use_layout expected #{@expected.inspect}, got # {@actual.inspect}", @expected, @actual end def negeative_failure_message return "use_layout expected #{@expected.inspect} not to equal # {@actual.inspect}", @expected, @actual end end def use_layout(expected) UseLayout.new(expected) end # in controller spec response.should use_layout("application") On May 29, 2007, at 6:19 PM, David Chelimsky wrote:> On 5/29/07, Jonathan Linowes <jonathan at parkerhill.com> wrote: >> to answer my own question here, @layout is a variable so >> >> response.layout.should == ''layouts/pages'' > > This is a great case for a custom matcher, if you''re interested. > Something like: > > response.should use_layout(''pages'') > > Check out http://blog.davidchelimsky.net/articles/2007/02/18/custom- > expectation-matchers > for info on how-to. > >> >> >> >> On May 29, 2007, at 11:57 AM, Jonathan Linowes wrote: >> >>> Hi, >>> >>> yes I realize I can assert the view template. I''m trying to assert >>> the layout one. >>> >>> I''ve tried >>> response.should render_template("pages") >>> and >>> response.should render_template("layouts/pages") >>> and >>> response.should render_template("/layouts/pages") >>> and >>> response.should render_template(''/app/views/layouts/site_pages'') >>> >>> >>> On May 29, 2007, at 8:36 AM, Anthony Carlos wrote: >>> >>>> >>>> On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote: >>>> >>>>> Hi, rather than flooding this list with separate emails, I''ve been >>>>> queueing up questions using rspec with rails. (an irc channel >>>>> would >>>>> be nice too :) Here ya go... >>>>> >>>>> - Can you assert which layout template is rendered in a controller >>>>> spec? >>>>> >>>> >>>> I''ll answer this one: >>>> >>>> response.should render_template() >>>> >>>> Don''t forget to include either a GET or POST, and if you''re using >>>> stubs or mocks, watch test.log for errors. I''ve noticed that when I >>>> stub out a model object, for example, if I don''t stub out all the >>>> methods that get called before the action gets to my render, >>>> then the >>>> spec fails but without a lot of related info. test.log will usually >>>> give me a hint with a stack trace. >>>> >>>> It''s documented here: http://rspec.rubyforge.org/documentation/ >>>> rails/ >>>> writing/controllers.html >>>> >>>>> >>>>> That''s it for now :) thanks >>>>> linoj >>>>> >>>>> >>>>> _______________________________________________ >>>>> rspec-users mailing list >>>>> rspec-users at rubyforge.org >>>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On 5/29/07, Jonathan Linowes <jonathan at parkerhill.com> wrote:> cool! it works!!Right on!> I put it all in spec_helper.rb > Is there a better place to put it? your post implies the class and > the method should be in separate files? (I dont know what is meant by > "#included in specify blocks")The Matchers module gets included in the object in which the examples get run, giving you access to matchers in that module in the examples. Both the Equal class and the equal method are defined in a single file. As for where to keep them, I do keep them in spec_helper, but you can certainly put them in a spec/matchers directory - or even a new plugin if you want to share them with your other apps.> http://blog.davidchelimsky.net/articles/2007/02/18/expectation-matchers > > # in spec_helper.rb > > class UseLayout > def initialize(expected) > @expected = ''layouts/'' + expected > end > def matches?(controller) > @actual = controller.layout > #@actual.equal?(@expected) > @actual == @expected > end > def failure_message > return "use_layout expected #{@expected.inspect}, got # > {@actual.inspect}", @expected, @actual > end > def negeative_failure_message > return "use_layout expected #{@expected.inspect} not to equal # > {@actual.inspect}", @expected, @actual > end > end > > > def use_layout(expected) > UseLayout.new(expected) > end > > # in controller spec > response.should use_layout("application") > > > On May 29, 2007, at 6:19 PM, David Chelimsky wrote: > > > On 5/29/07, Jonathan Linowes <jonathan at parkerhill.com> wrote: > >> to answer my own question here, @layout is a variable so > >> > >> response.layout.should == ''layouts/pages'' > > > > This is a great case for a custom matcher, if you''re interested. > > Something like: > > > > response.should use_layout(''pages'') > > > > Check out http://blog.davidchelimsky.net/articles/2007/02/18/custom- > > expectation-matchers > > for info on how-to. > > > >> > >> > >> > >> On May 29, 2007, at 11:57 AM, Jonathan Linowes wrote: > >> > >>> Hi, > >>> > >>> yes I realize I can assert the view template. I''m trying to assert > >>> the layout one. > >>> > >>> I''ve tried > >>> response.should render_template("pages") > >>> and > >>> response.should render_template("layouts/pages") > >>> and > >>> response.should render_template("/layouts/pages") > >>> and > >>> response.should render_template(''/app/views/layouts/site_pages'') > >>> > >>> > >>> On May 29, 2007, at 8:36 AM, Anthony Carlos wrote: > >>> > >>>> > >>>> On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote: > >>>> > >>>>> Hi, rather than flooding this list with separate emails, I''ve been > >>>>> queueing up questions using rspec with rails. (an irc channel > >>>>> would > >>>>> be nice too :) Here ya go... > >>>>> > >>>>> - Can you assert which layout template is rendered in a controller > >>>>> spec? > >>>>> > >>>> > >>>> I''ll answer this one: > >>>> > >>>> response.should render_template() > >>>> > >>>> Don''t forget to include either a GET or POST, and if you''re using > >>>> stubs or mocks, watch test.log for errors. I''ve noticed that when I > >>>> stub out a model object, for example, if I don''t stub out all the > >>>> methods that get called before the action gets to my render, > >>>> then the > >>>> spec fails but without a lot of related info. test.log will usually > >>>> give me a hint with a stack trace. > >>>> > >>>> It''s documented here: http://rspec.rubyforge.org/documentation/ > >>>> rails/ > >>>> writing/controllers.html > >>>> > >>>>> > >>>>> That''s it for now :) thanks > >>>>> linoj > >>>>> > >>>>> > >>>>> _______________________________________________ > >>>>> rspec-users mailing list > >>>>> rspec-users at rubyforge.org > >>>>> http://rubyforge.org/mailman/listinfo/rspec-users > >>>> > >>>> _______________________________________________ > >>>> rspec-users mailing list > >>>> rspec-users at rubyforge.org > >>>> http://rubyforge.org/mailman/listinfo/rspec-users > >>> > >>> _______________________________________________ > >>> rspec-users mailing list > >>> rspec-users at rubyforge.org > >>> http://rubyforge.org/mailman/listinfo/rspec-users > >> > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-users at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > >> > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Hi, revisiting this issue, I feel I''m getting closer: describe CommentsController, "named routes" do before do @post = mock_model(Post, :id => 22) end it "should generate post_comments_path" do controller.post_comments_path(@post).should == "/posts/22/comments" end end results in: NoMethodError in ''CommentsController named routes should generate post_comments_path'' protected method `post_comments_path'' called for #<CommentsController: 0x34ab3cc> Is there a way around this? Or how are other testing named routes? thanks linoj On May 29, 2007, at 7:52 AM, Scott Taylor wrote:> > On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote: > >> >> - Is there a way to test named routes? > > I don''t think there is a very clean & direct way (hopefully the > others on the list can correct me). The rspec rest generator > generates some code to check the routes, so you could use something > like that. > > Scott > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
try this: controller.send(:post_comments_path, @post).should ="/posts/22/comments" Rgds, Jerry -----Original Message----- From: rspec-users-bounces at rubyforge.org [mailto:rspec-users-bounces at rubyforge.org] On Behalf Of Jonathan Linowes Sent: 15 August 2007 21:03 To: rspec-users Subject: [rspec-users] restful named routes Hi, revisiting this issue, I feel I''m getting closer: describe CommentsController, "named routes" do before do @post = mock_model(Post, :id => 22) end it "should generate post_comments_path" do controller.post_comments_path(@post).should == "/posts/22/comments" end end results in: NoMethodError in ''CommentsController named routes should generate post_comments_path'' protected method `post_comments_path'' called for #<CommentsController: 0x34ab3cc> Is there a way around this? Or how are other testing named routes? thanks linoj On May 29, 2007, at 7:52 AM, Scott Taylor wrote:> > On May 29, 2007, at 4:54 AM, Jonathan Linowes wrote: > >> >> - Is there a way to test named routes? > > I don''t think there is a very clean & direct way (hopefully the others > on the list can correct me). The rspec rest generator generates some > code to check the routes, so you could use something like that. > > Scott > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users_______________________________________________ rspec-users mailing list rspec-users at rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users