From osahyoun at gmail.com Sat Dec 1 11:42:50 2007 From: osahyoun at gmail.com (Sahyoun) Date: Sat, 1 Dec 2007 17:42:50 +0100 Subject: [rspec-users] mock feed Message-ID: Hello generous coders, I'm having difficulty getting a couple of tests to pass. I have an address model which takes an address and with a callback on save and update passes the address to google's geocoding service. The returned XML is parsed and saved to the address object: protected def geocode_address key = "mykey" url = "http://maps.google.com/maps/geo?q=#{self.address_1.gsub(' ', '+')},+#{self.postcode},+FRANCE&output=xml&key=#{key}" doc = Hpricot.XML(Iconv.iconv("UTF-8", "ISO-8859-2", open(URI.escape (url)).read).to_s) section = (doc/"Placemark").first self.lat, self.lng = (section/"coordinates").text.split(',') self.town = (section/"LocalityName").text self.address_1 = (section/"ThoroughfareName").text end In my spec for the address model I have as a helper: def mock_feed directions = "q=71+av+Parmentier,+75011,+FRANCE&output=xml&key=mykey" xml = File.read(RAILS_ROOT + "/spec/fixtures/feeds/75001.xml") @address.should_receive(:open).exactly(1).times. with("http://maps.google.com/maps/geo?#{directions}"). and_return(xml) end ....................... describe Address, "fetch and geocode" do include AddressSpecHelper before do @address = Address.new mock_feed @address.attributes = valid_address_attributes @address.save end it "should populate town and address 1" do @address.town.should eql("Paris") @address.address_1.should eql("71, Avenue Parmentier") end it "should populate lat and lng as BigDecimals" do @address.lat.should eql(BigDecimal("2.375258")) @address.lng.should eql(BigDecimal("48.864315")) end end My two errors: 2) NoMethodError in 'Address fetch and geocode should populate town and address 1' undefined method `read' for # 1) NoMethodError in 'Address fetch and geocode should populate lat and lng as BigDecimals' undefined method `read' for # These tests passed fine with a previous version of the geocode_address method: protected def geocode_address key = "mykey" url = "http://maps.google.com/maps/geo?q=#{self.address_1.gsub(' ', '+')},+#{self.postcode},+FRANCE&output=xml&key=#{key}" doc = Hpricot.XML(open(url)) self.lat, self.lng = (doc/"coordinates").text.split(',') self.town = (doc/"LocalityName").text self.address_1 = (doc/"ThoroughfareName").text end I'm new to rspec and the ideas behind mocks and stubs. I don't understand why rspec is paying any notice to 'read'. Do I also need to inform the mock of the 'read' method? If so, how and why? Any pointers would be gratefully received. Many thanks O -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071201/40018e75/attachment-0001.html From sera at fhwang.net Sat Dec 1 15:45:16 2007 From: sera at fhwang.net (Francis Hwang) Date: Sat, 1 Dec 2007 15:45:16 -0500 Subject: [rspec-users] specifying an HTTP status code Message-ID: Hi all, I'm just starting to work w/ RSpec, so I hope this question isn't too obvious or missing the point somehow: Is there a way in RSpec to specify that a controller action should use a specific HTTP status code? Specifically I want to test for the usage of 301 as opposed to 302, for a permanent redirection. response.should be_redirect looks like it calls ActionController::TestRequest#redirect?, which is only testing for a status code from 300..399. In an old-fashioned Test::Unit Rails test I could do assert_response, but I can't see anything matching that in RSpec. Is there a less-documented feature I'm missing? Thanks, Francis Hwang http://fhwang.net/ From mailing_lists at railsnewbie.com Sat Dec 1 16:27:53 2007 From: mailing_lists at railsnewbie.com (Scott Taylor) Date: Sat, 1 Dec 2007 16:27:53 -0500 Subject: [rspec-users] specifying an HTTP status code In-Reply-To: References: Message-ID: <810787FC-7AD1-4638-8943-CBA5DED11D2A@railsnewbie.com> On Dec 1, 2007, at 3:45 PM, Francis Hwang wrote: > Hi all, > > I'm just starting to work w/ RSpec, so I hope this question isn't too > obvious or missing the point somehow: Is there a way in RSpec to > specify that a controller action should use a specific HTTP status > code? Specifically I want to test for the usage of 301 as opposed to > 302, for a permanent redirection. > > response.should be_redirect > > looks like it calls ActionController::TestRequest#redirect?, which is > only testing for a status code from 300..399. In an old-fashioned > Test::Unit Rails test I could do assert_response, but I can't see > anything matching that in RSpec. Is there a less-documented feature > I'm missing? > > Thanks, > > Francis Hwang > http://fhwang.net/ > Hey Francis - Great to see you've finally come over from the dark side (Test::Unit)! How is rspec treating you? Anyway - here's how I've done it with a 404: describe ErrorController, "view" do it "should render the 404 page in public/" do get :view response.should render_template("#{RAILS_ROOT}/public/404.html") end it "should return an HTTP status code of 404" do get :view response.headers["Status"].should == "404 Not Found" end end I would imagine you could do something similar for 301. I don't think there are currently any matchers for these status codes like: should_be_not_found or should_not_be_a_404, although they could certainly be written. (Method missing seems to be screaming at me here...) Regards, Scott Taylor From dchelimsky at gmail.com Sat Dec 1 17:08:47 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 1 Dec 2007 17:08:47 -0500 Subject: [rspec-users] specifying an HTTP status code In-Reply-To: <810787FC-7AD1-4638-8943-CBA5DED11D2A@railsnewbie.com> References: <810787FC-7AD1-4638-8943-CBA5DED11D2A@railsnewbie.com> Message-ID: <57c63afe0712011408q268be4dsb56738ef25010822@mail.gmail.com> On Dec 1, 2007 4:27 PM, Scott Taylor wrote: > > On Dec 1, 2007, at 3:45 PM, Francis Hwang wrote: > > > Hi all, > > > > I'm just starting to work w/ RSpec, so I hope this question isn't too > > obvious or missing the point somehow: Is there a way in RSpec to > > specify that a controller action should use a specific HTTP status > > code? Specifically I want to test for the usage of 301 as opposed to > > 302, for a permanent redirection. > > > > response.should be_redirect > > > > looks like it calls ActionController::TestRequest#redirect?, which is > > only testing for a status code from 300..399. In an old-fashioned > > Test::Unit Rails test I could do assert_response, but I can't see > > anything matching that in RSpec. Is there a less-documented feature > > I'm missing? > > > > Thanks, > > > > Francis Hwang > > http://fhwang.net/ > > > > Hey Francis - Great to see you've finally come over from the dark > side (Test::Unit)! How is rspec treating you? > > Anyway - here's how I've done it with a 404: > > describe ErrorController, "view" do > > it "should render the 404 page in public/" do > get :view > response.should render_template("#{RAILS_ROOT}/public/404.html") > end > > it "should return an HTTP status code of 404" do > get :view > response.headers["Status"].should == "404 Not Found" > end > > end You can also just go right for the response_code: response.response_code.should == 301 > > > I would imagine you could do something similar for 301. I don't > think there are currently any matchers for these status codes like: > should_be_not_found or should_not_be_a_404, although they could > certainly be written. (Method missing seems to be screaming at me > here...) > > Regards, > > Scott Taylor > > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From jarkko at jlaine.net Sat Dec 1 17:10:36 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Sun, 2 Dec 2007 00:10:36 +0200 Subject: [rspec-users] specifying an HTTP status code In-Reply-To: <810787FC-7AD1-4638-8943-CBA5DED11D2A@railsnewbie.com> References: <810787FC-7AD1-4638-8943-CBA5DED11D2A@railsnewbie.com> Message-ID: <368DA940-A278-42B1-ADF1-14411C233BA7@jlaine.net> On 1.12.2007, at 23.27, Scott Taylor wrote: > it "should return an HTTP status code of 404" do > get :view > response.headers["Status"].should == "404 Not Found" or response.response_code.should == 404 -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From mailing_lists at railsnewbie.com Sat Dec 1 17:19:20 2007 From: mailing_lists at railsnewbie.com (Scott Taylor) Date: Sat, 1 Dec 2007 17:19:20 -0500 Subject: [rspec-users] specifying an HTTP status code In-Reply-To: <368DA940-A278-42B1-ADF1-14411C233BA7@jlaine.net> References: <810787FC-7AD1-4638-8943-CBA5DED11D2A@railsnewbie.com> <368DA940-A278-42B1-ADF1-14411C233BA7@jlaine.net> Message-ID: <27B6BA7F-96B2-480B-8F7D-8892F35F8B3A@railsnewbie.com> On Dec 1, 2007, at 5:10 PM, Jarkko Laine wrote: > On 1.12.2007, at 23.27, Scott Taylor wrote: >> it "should return an HTTP status code of 404" do >> get :view >> response.headers["Status"].should == "404 Not Found" > > or > > response.response_code.should == 404 > Nice. I find out something new every day from this list. From cdemyanovich at gmail.com Sun Dec 2 08:12:14 2007 From: cdemyanovich at gmail.com (Craig Demyanovich) Date: Sun, 2 Dec 2007 08:12:14 -0500 Subject: [rspec-users] specifying an HTTP status code In-Reply-To: <27B6BA7F-96B2-480B-8F7D-8892F35F8B3A@railsnewbie.com> References: <810787FC-7AD1-4638-8943-CBA5DED11D2A@railsnewbie.com> <368DA940-A278-42B1-ADF1-14411C233BA7@jlaine.net> <27B6BA7F-96B2-480B-8F7D-8892F35F8B3A@railsnewbie.com> Message-ID: <61c885db0712020512m59805edla196d99ed212670d@mail.gmail.com> On Dec 1, 2007 5:19 PM, Scott Taylor wrote: > > On Dec 1, 2007, at 5:10 PM, Jarkko Laine wrote: > > > On 1.12.2007, at 23.27, Scott Taylor wrote: > >> it "should return an HTTP status code of 404" do > >> get :view > >> response.headers["Status"].should == "404 Not Found" > > > > or > > > > response.response_code.should == 404 > > > > Nice. I find out something new every day from this list. You might also look at status_codes.rb. For example, if you have rails in vendor/rails, it's path is vendor/rails/actionpack/lib/action_controller/status_codes.rb For any status in that file, you should be able to do this in your examples: assert_response :not_found Yes, that's a Test::Unit assertion. Here's why I use it: * I'm not sure if there is a way to do it in RSpec (haven't yet investigated) * I can use Test::Unit assertions in RSpec (really cool) * I like the way it reads compared to using the codes, such as 404 Regards, Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071202/bd0a0834/attachment.html From daniel at helpmebuyacar.org Mon Dec 3 04:57:35 2007 From: daniel at helpmebuyacar.org (Fischer, Daniel) Date: Mon, 3 Dec 2007 01:57:35 -0800 Subject: [rspec-users] Need help mocking this out Message-ID: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> Let's say you're using the restful_authentication plugin. You have a model called articles. On the index action of the articlescontroller you simply want to spec out that it'll scope the results to the ownership of the current_user. It should NOT include any articles other than the articles that user owns. How would you properly spec this out? Thanks for the help! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/1d33f697/attachment.html From daniel at danielfischer.com Mon Dec 3 04:58:16 2007 From: daniel at danielfischer.com (Fischer, Daniel) Date: Mon, 3 Dec 2007 01:58:16 -0800 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> Message-ID: <7e565b5c0712030158m64d0a3fr4c7218508ecec201@mail.gmail.com> Let's say you're using the restful_authentication plugin. You have a model called articles. On the index action of the articlescontroller you simply want to spec out that it'll scope the results to the ownership of the current_user. It should NOT include any articles other than the articles that user owns. How would you properly spec this out? Thanks for the help! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/362a4bab/attachment.html From stefan.landro at gmail.com Mon Dec 3 05:07:51 2007 From: stefan.landro at gmail.com (=?ISO-8859-1?Q?Stefan_Magnus_Landr=F8?=) Date: Mon, 3 Dec 2007 11:07:51 +0100 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> Message-ID: <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> Typically, I'd write a method in your user model that returns the user's articles: class User do def find_articles_for_user Article.find(:all, :conditions => ['userid = ?', id) end end Then you'd use a mock in your controller spec, and make sure you test that your method is being called. On the other hand, the user model should be tested directly against the db. HTH, Stefan 2007/12/3, Fischer, Daniel : > > Let's say you're using the restful_authentication plugin. > You have a model called articles. On the index action of the > articlescontroller you simply want to spec out that it'll scope the results > to the ownership of the current_user. > > It should NOT include any articles other than the articles that user owns. > > How would you properly spec this out? > > Thanks for the help! > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- Bekk Open Source http://boss.bekk.no -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/ca3fda82/attachment.html From me at danielfischer.com Mon Dec 3 05:33:44 2007 From: me at danielfischer.com (Fischer, Daniel) Date: Mon, 3 Dec 2007 02:33:44 -0800 Subject: [rspec-users] Need help mocking this out In-Reply-To: <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> Message-ID: <7e565b5c0712030233h9554c98h57fd1f90d4bcf023@mail.gmail.com> yuck, that seems kind of nasty, no? user.articles is already scoped... There has to be a different solution! On Dec 3, 2007 2:07 AM, Stefan Magnus Landr? wrote: > Typically, I'd write a method in your user model that returns the user's > articles: > > class User do > > def find_articles_for_user > Article.find(:all, :conditions => ['userid = ?', id) > end > > end > > Then you'd use a mock in your controller spec, and make sure you test that > your method is being called. > > On the other hand, the user model should be tested directly against the > db. > > HTH, > > Stefan > > 2007/12/3, Fischer, Daniel : > > > > Let's say you're using the restful_authentication plugin. > > You have a model called articles. On the index action of the > > articlescontroller you simply want to spec out that it'll scope the results > > to the ownership of the current_user. > > > > It should NOT include any articles other than the articles that user > > owns. > > > > How would you properly spec this out? > > > > Thanks for the help! > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > -- > Bekk Open Source > http://boss.bekk.no > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/78dbb660/attachment-0001.html From jarkko at jlaine.net Mon Dec 3 05:42:46 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Mon, 3 Dec 2007 12:42:46 +0200 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> Message-ID: <5A47E0E4-9A00-47D2-8FF1-BDA4F3849637@jlaine.net> On 3.12.2007, at 11.57, Fischer, Daniel wrote: > Let's say you're using the restful_authentication plugin. > > You have a model called articles. On the index action of the > articlescontroller you simply want to spec out that it'll scope the > results to the ownership of the current_user. > > It should NOT include any articles other than the articles that user > owns. Given that you have user has_many :articles You want to use current_user.articles (or current_user.articles.find(*additional options*)). In the spec, you want to make sure that the articles are scoped through the correct user: before(:each) do @user = mock_model(User) User.stub!(:find).and_return(@user) # to make sure current_user is the mock model @articles = [mock_model(Article)] end it "should find articles for the current user" do @user.should_receive(:articles).and_return(@articles) get :index end It is then the responsibility of the articles association to scope the results. In the controller specs you just trust that it works. -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From has.sox at gmail.com Mon Dec 3 05:44:06 2007 From: has.sox at gmail.com (Daniel N) Date: Mon, 3 Dec 2007 21:44:06 +1100 Subject: [rspec-users] Need help mocking this out In-Reply-To: <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> Message-ID: <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> Assuming that there is a call like this in your controller @articles = current_user.articles One way to do this is to stub out the controller.current_user to return a mock object of the current_user Then put an expectation on the current user that it's articles method gets called. (return a mocked collection of articles) Then check that @articles is set to the returned mocked collection of articles from current_user.articles phew... Ok So one way you might write this could be (This is untested...) it "should scope the articles to the currrent_user" do user = mock_model(User) articles = [mock_model(Article)] controller.stub!(:current_user).and_return(user) user.should_receive(:articles).and_return(articles) get :index assigns[:articles].should == articles end Like I said though, that's not tested itself. If that's not exactly right... it's along the right track of an option that can work. HTH Daniel On Dec 3, 2007 9:07 PM, Stefan Magnus Landr? wrote: > Typically, I'd write a method in your user model that returns the user's > articles: > > class User do > > def find_articles_for_user > Article.find(:all, :conditions => ['userid = ?', id) > end > > end > > Then you'd use a mock in your controller spec, and make sure you test that > your method is being called. > > On the other hand, the user model should be tested directly against the > db. > > HTH, > > Stefan > > 2007/12/3, Fischer, Daniel : > > > > Let's say you're using the restful_authentication plugin. > > You have a model called articles. On the index action of the > > articlescontroller you simply want to spec out that it'll scope the results > > to the ownership of the current_user. > > > > It should NOT include any articles other than the articles that user > > owns. > > > > How would you properly spec this out? > > > > Thanks for the help! > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > -- > Bekk Open Source > http://boss.bekk.no > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/cffdf875/attachment.html From daniel.ruby at tenner.org Mon Dec 3 05:48:16 2007 From: daniel.ruby at tenner.org (Daniel Tenner) Date: Mon, 3 Dec 2007 10:48:16 +0000 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712030233h9554c98h57fd1f90d4bcf023@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <7e565b5c0712030233h9554c98h57fd1f90d4bcf023@mail.gmail.com> Message-ID: Hi Daniel, You're trying to do too much in the controller. It's not the controller's responsibility to ensure that the user is capable of returning its own article without including anyone else's - that's the user's (or the Article model's, perhaps) responsibility. Your controller should be thin and simple, delegating all business logic onto the model objects. See this article: http://www.pragprog.com/ articles/tell-dont-ask Once thinned down like this, speccing the controller becomes trivial - just mock the current_user method and ensure that it receives a call to "available_articles" or whatever the method name is. Then, spec that method in the User model. At that point, don't mock anything you don't have to, and test the behaviour of the User model (e.g. "it should not return anyone else's article within its available articles" or something like that). Hope this helps, Daniel On 3 Dec 2007, at 10:33 3 Dec 2007, Fischer, Daniel wrote: > yuck, that seems kind of nasty, no? > > user.articles is already scoped... > > There has to be a different solution! > > On Dec 3, 2007 2:07 AM, Stefan Magnus Landr? > wrote: > Typically, I'd write a method in your user model that returns the > user's articles: > > class User do > > def find_articles_for_user > Article.find(:all, :conditions => ['userid = ?', id) > end > > end > > Then you'd use a mock in your controller spec, and make sure you > test that your method is being called. > > On the other hand, the user model should be tested directly against > the db. > > HTH, > > Stefan > > 2007/12/3, Fischer, Daniel : > Let's say you're using the restful_authentication plugin. > > You have a model called articles. On the index action of the > articlescontroller you simply want to spec out that it'll scope the > results to the ownership of the current_user. > > It should NOT include any articles other than the articles that > user owns. > > How would you properly spec this out? > > Thanks for the help! > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > > -- > Bekk Open Source > http://boss.bekk.no > _______________________________________________ > 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/8555842d/attachment.html From stefan.landro at gmail.com Mon Dec 3 06:50:15 2007 From: stefan.landro at gmail.com (=?ISO-8859-1?Q?Stefan_Magnus_Landr=F8?=) Date: Mon, 3 Dec 2007 12:50:15 +0100 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712030233h9554c98h57fd1f90d4bcf023@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <7e565b5c0712030233h9554c98h57fd1f90d4bcf023@mail.gmail.com> Message-ID: <921ca2f80712030350p6f6b374aoc2afb7322c63e1a3@mail.gmail.com> Well, if articles is already scoped, you just say: user.should_receive('articles').and_return(some_articles) To make sure your articles method is scoped, write a model test that hits the database, and verify that only the user's articles are returned. As Daniel Tenner says, you should try to keep your controllers as thin as possible. In addition you should try to keep your tests as fast as possible - and you typically do that by not hitting the DB. Stefan 2007/12/3, Fischer, Daniel : > > yuck, that seems kind of nasty, no? > user.articles is already scoped... > > There has to be a different solution! > > On Dec 3, 2007 2:07 AM, Stefan Magnus Landr? > wrote: > > > Typically, I'd write a method in your user model that returns the user's > > articles: > > > > class User do > > > > def find_articles_for_user > > Article.find(:all, :conditions => ['userid = ?', id) > > end > > > > end > > > > Then you'd use a mock in your controller spec, and make sure you test > > that your method is being called. > > > > On the other hand, the user model should be tested directly against the > > db. > > > > HTH, > > > > Stefan > > > > 2007/12/3, Fischer, Daniel : > > > > > > Let's say you're using the restful_authentication plugin. > > > You have a model called articles. On the index action of the > > > articlescontroller you simply want to spec out that it'll scope the results > > > to the ownership of the current_user. > > > > > > It should NOT include any articles other than the articles that user > > > owns. > > > > > > How would you properly spec this out? > > > > > > Thanks for the help! > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > -- > > Bekk Open Source > > http://boss.bekk.no > > _______________________________________________ > > 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 > -- Bekk Open Source http://boss.bekk.no -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/9cb5dd5a/attachment-0001.html From bryan at osesm.com Mon Dec 3 08:32:13 2007 From: bryan at osesm.com (Bryan Liles) Date: Mon, 3 Dec 2007 08:32:13 -0500 Subject: [rspec-users] Stylistic preferences In-Reply-To: References: Message-ID: <9C6B87F4-C473-4B23-8176-19A9A5165AB0@osesm.com> On Nov 29, 2007, at 5:54 AM, Daniel Tenner wrote: > What are people's opinions on which of these two styles is better to > use? > > > it "should be possible to disable the number" do > given(valid_sms_user) do |user| > user.save > user.disable_number > user.should be_disabled > end > end > it "should be possible to disable the number" do lambda { user.save user.disable_number }.should change(user, :disabled?).from(true).to(false) end If this doesn't work, the solution is very close to this. From daniel.ruby at tenner.org Mon Dec 3 08:42:42 2007 From: daniel.ruby at tenner.org (Daniel Tenner) Date: Mon, 3 Dec 2007 13:42:42 +0000 Subject: [rspec-users] Stylistic preferences In-Reply-To: <9C6B87F4-C473-4B23-8176-19A9A5165AB0@osesm.com> References: <9C6B87F4-C473-4B23-8176-19A9A5165AB0@osesm.com> Message-ID: <096B6EDB-E5A4-41D1-B619-B4B63DC48ABE@tenner.org> The code works fine. I was asking about the "given" thing. Daniel On 3 Dec 2007, at 13:32 3 Dec 2007, Bryan Liles wrote: > > On Nov 29, 2007, at 5:54 AM, Daniel Tenner wrote: > >> What are people's opinions on which of these two styles is better to >> use? >> >> >> it "should be possible to disable the number" do >> given(valid_sms_user) do |user| >> user.save >> user.disable_number >> user.should be_disabled >> end >> end >> > > > it "should be possible to disable the number" do > lambda { > user.save > user.disable_number > }.should change(user, :disabled?).from(true).to(false) > end > > If this doesn't work, the solution is very close to this. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From stefan.landro at gmail.com Mon Dec 3 10:31:03 2007 From: stefan.landro at gmail.com (=?ISO-8859-1?Q?Stefan_Magnus_Landr=F8?=) Date: Mon, 3 Dec 2007 16:31:03 +0100 Subject: [rspec-users] Stylistic preferences In-Reply-To: References: Message-ID: <921ca2f80712030731w35d88e95p6cdc957c240e32f8@mail.gmail.com> Well, I kinda like the word given, however I feel it's competing with the Context in describe Class, "Context" do Maybe before should be replaced with some givens and expectations, like so: describe Class, "Context" do given do end always_expect do end it "should ..." do end end Stefan 2007/11/29, Daniel Tenner : > > What are people's opinions on which of these two styles is better to > use? > > 1) before > --------------------------- > module UserSpecHelper > include GenericSpecHelper > > def valid_sms_attributes(phone_number="12345") > { :phone_number => phone_number } > end > end > > describe User, "with phone number" do > include UserSpecHelper > > before(:each) do > @user = User.new(valid_sms_attributes) > end > > it "should be valid" do > @user.should be_valid > end > > it "should reject duplicate phone number" do > @user.save > @user_2 = User.new(valid_sms_attributes) > @user_2.should_not be_valid > end > > it "should be possible to disable the number" do > @user.save > @user.disable_number > @user.should be_disabled > end > end > > > ------------------------ > 2) given/yield > --------------------------- > module GenericSpecHelper > def given(thing) > yield thing if block_given? > thing > end > > end > > > module UserSpecHelper > include GenericSpecHelper > def valid_sms_attributes(phone_number="12345") > { :phone_number => phone_number } > end > > def valid_sms_user(phone_number="12345") > User.new(valid_sms_attributes(phone_number)) > end > end > > > describe User, "unconfirmed" do > include UserSpecHelper > > it "should be valid" do > valid_sms_user.should be_valid > end > > it "should reject duplicate phone number" do > valid_sms_user("1").save > valid_sms_user("1").should_not be_valid > end > > it "should be possible to disable the number" do > given(valid_sms_user) do |user| > user.save > user.disable_number > user.should be_disabled > end > end > > > end > ------------------- > > My thoughts: the second style is more readable sometimes, less other > times. More importantly, with the first style, my specs tend to be > split alongside the lines of whether they can use the same before > (:each), rather than whether they belong together, whereas with the > second one, one "description" can have several different "starting > points", and I group them by whether I feel they belong together > logically. So at the moment I'm more comfortable with the second style. > > What do you think? > > Daniel > > PS: I know these specs themselves are trivial... I've used both > approaches in less trivial specs, I hope these illustrate the idea > though > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- Bekk Open Source http://boss.bekk.no -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/8f82b69b/attachment.html From osahyoun at gmail.com Mon Dec 3 13:33:56 2007 From: osahyoun at gmail.com (Sahyoun) Date: Mon, 3 Dec 2007 19:33:56 +0100 Subject: [rspec-users] spec for model_id should eql Message-ID: Hello, I'm confused why the spec described below is failing. Other simple comparison specs are passing fine for the same model. The code is working accordingly when I test it through the console, I'm just having difficulty getting this spec to work. Any pointers would be appreciated. Failure message: 'Address fetch and geocode should extract department and write to address object' FAILED expected 3, got nil (using .eql?) This spec that is failing: @address.department_id.should eql(3) before: before do @address = Address.new @address.attributes = valid_address_attributes @address.save @department = mock_model(Department) @department.stub!(:find_by_code).with("75").and_return("3") end The line of code I am writing the spec for: self.department = Department.find_by_code(self.postcode[0..1]) Many thanks, Omar -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/0b8e5734/attachment.html From nathan.sutton at gmail.com Mon Dec 3 14:05:28 2007 From: nathan.sutton at gmail.com (Nathan Sutton) Date: Mon, 3 Dec 2007 13:05:28 -0600 Subject: [rspec-users] spec for model_id should eql In-Reply-To: References: Message-ID: <307B2AF4-E9C4-427C-BBCD-DD51EC6D6023@gmail.com> Department.stub!(:find_by_code).with("75").and_return("3") Nathan Sutton fowlduck at gmail.com rspec edge revision 3014 rspec_on_rails edge revision 3014 rails edge revision 8238 On Dec 3, 2007, at 12:33 PM, Sahyoun wrote: > Hello, > > I'm confused why the spec described below is failing. Other simple > comparison specs are passing fine for the same model. The code is > working accordingly when I test it through the console, I'm just > having difficulty getting this spec to work. Any pointers would be > appreciated. > > Failure message: > 'Address fetch and geocode should extract department and write to > address object' FAILED > expected 3, got nil (using .eql?) > > This spec that is failing: > @address.department_id.should eql(3) > > before: > before do > @address = Address.new > @address.attributes = valid_address_attributes > @address.save > @department = mock_model(Department) > @department.stub! (:find_by_code).with("75").and_return("3") > end > > > The line of code I am writing the spec for: > self.department = Department.find_by_code(self.postcode[0..1]) > > > > Many thanks, > Omar > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From jarkko at jlaine.net Mon Dec 3 14:18:35 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Mon, 3 Dec 2007 21:18:35 +0200 Subject: [rspec-users] spec for model_id should eql In-Reply-To: <307B2AF4-E9C4-427C-BBCD-DD51EC6D6023@gmail.com> References: <307B2AF4-E9C4-427C-BBCD-DD51EC6D6023@gmail.com> Message-ID: <1533E5F7-ADF1-44B1-8B51-13977627E3C2@jlaine.net> On 3.12.2007, at 21.05, Nathan Sutton wrote: > Department.stub!(:find_by_code).with("75").and_return("3") You can't use with() with stub! (only with should_receive), so just leave it away: Department.stub!(:find_by_code).and_return("3") //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From dchelimsky at gmail.com Mon Dec 3 14:23:01 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 3 Dec 2007 13:23:01 -0600 Subject: [rspec-users] spec for model_id should eql In-Reply-To: <1533E5F7-ADF1-44B1-8B51-13977627E3C2@jlaine.net> References: <307B2AF4-E9C4-427C-BBCD-DD51EC6D6023@gmail.com> <1533E5F7-ADF1-44B1-8B51-13977627E3C2@jlaine.net> Message-ID: <57c63afe0712031123r72adc566i5b131966c423e2a2@mail.gmail.com> On Dec 3, 2007 1:18 PM, Jarkko Laine wrote: > > On 3.12.2007, at 21.05, Nathan Sutton wrote: > > > Department.stub!(:find_by_code).with("75").and_return("3") > > You can't use with() with stub! (only with should_receive), so just > leave it away: > > Department.stub!(:find_by_code).and_return("3") Actually, you can now, but it's not really documented. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From nathan.sutton at gmail.com Mon Dec 3 14:28:06 2007 From: nathan.sutton at gmail.com (Nathan Sutton) Date: Mon, 3 Dec 2007 13:28:06 -0600 Subject: [rspec-users] spec for model_id should eql In-Reply-To: <57c63afe0712031123r72adc566i5b131966c423e2a2@mail.gmail.com> References: <307B2AF4-E9C4-427C-BBCD-DD51EC6D6023@gmail.com> <1533E5F7-ADF1-44B1-8B51-13977627E3C2@jlaine.net> <57c63afe0712031123r72adc566i5b131966c423e2a2@mail.gmail.com> Message-ID: <7A34AA27-981A-4E19-B8E0-F8968CA3E2CF@gmail.com> It's worked since I started using rspec, but I started relatively recently, and only on edge. Nathan Sutton fowlduck at gmail.com rspec edge revision 3014 rspec_on_rails edge revision 3014 rails edge revision 8238 On Dec 3, 2007, at 1:23 PM, David Chelimsky wrote: > On Dec 3, 2007 1:18 PM, Jarkko Laine wrote: >> >> On 3.12.2007, at 21.05, Nathan Sutton wrote: >> >>> Department.stub!(:find_by_code).with("75").and_return("3") >> >> You can't use with() with stub! (only with should_receive), so just >> leave it away: >> >> Department.stub!(:find_by_code).and_return("3") > > Actually, you can now, but it's not really documented. > >> >> //jarkko >> >> -- >> Jarkko Laine >> http://jlaine.net >> http://dotherightthing.com >> http://www.railsecommerce.com >> http://odesign.fi >> >> >> >> _______________________________________________ >> 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 From jarkko at jlaine.net Mon Dec 3 15:34:04 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Mon, 3 Dec 2007 22:34:04 +0200 Subject: [rspec-users] spec for model_id should eql In-Reply-To: <57c63afe0712031123r72adc566i5b131966c423e2a2@mail.gmail.com> References: <307B2AF4-E9C4-427C-BBCD-DD51EC6D6023@gmail.com> <1533E5F7-ADF1-44B1-8B51-13977627E3C2@jlaine.net> <57c63afe0712031123r72adc566i5b131966c423e2a2@mail.gmail.com> Message-ID: <8DC76CCC-3E88-473B-891F-5EF46AD9E26E@jlaine.net> On 3.12.2007, at 21.23, David Chelimsky wrote: > On Dec 3, 2007 1:18 PM, Jarkko Laine wrote: >> >> On 3.12.2007, at 21.05, Nathan Sutton wrote: >> >>> Department.stub!(:find_by_code).with("75").and_return("3") >> >> You can't use with() with stub! (only with should_receive), so just >> leave it away: >> >> Department.stub!(:find_by_code).and_return("3") > > Actually, you can now, but it's not really documented. Oh, cool. I take it back, then :-) //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From me at danielfischer.com Mon Dec 3 23:26:28 2007 From: me at danielfischer.com (Fischer, Daniel) Date: Mon, 3 Dec 2007 20:26:28 -0800 Subject: [rspec-users] Need help mocking this out In-Reply-To: <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> Message-ID: <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> Hey cool, thanks for the help guys. One problem though, when I take this approach I can't decouple the specs anymore. They all "User_xxx receive unexpected message :articles". It seems silly to include all behaviors in one spec, or put that expectation in each test. Is there a way around this? Thanks for all the help, Daniel Fischer http://www.danielfischer.com On Dec 3, 2007 2:44 AM, Daniel N wrote: > Assuming that there is a call like this in your controller > @articles = current_user.articles > > One way to do this is to stub out the controller.current_user to return a > mock object of the current_user > > Then put an expectation on the current user that it's articles method gets > called. (return a mocked collection of articles) > > Then check that @articles is set to the returned mocked collection of > articles from current_user.articles > > phew... > > Ok So one way you might write this could be (This is untested...) > > it "should scope the articles to the currrent_user" do > > user = mock_model(User) > articles = [mock_model(Article)] > > controller.stub!(:current_user).and_return(user) > user.should_receive (:articles).and_return(articles) > > get :index > > assigns[:articles].should == articles > > end > > Like I said though, that's not tested itself. If that's not exactly > right... it's along the right track of an option that can work. > > HTH > Daniel > > On Dec 3, 2007 9:07 PM, Stefan Magnus Landr? > wrote: > > > Typically, I'd write a method in your user model that returns the user's > > articles: > > > > class User do > > > > def find_articles_for_user > > Article.find(:all, :conditions => ['userid = ?', id) > > end > > > > end > > > > Then you'd use a mock in your controller spec, and make sure you test > > that your method is being called. > > > > On the other hand, the user model should be tested directly against the > > db. > > > > HTH, > > > > Stefan > > > > 2007/12/3, Fischer, Daniel : > > > > > > Let's say you're using the restful_authentication plugin. > > > You have a model called articles. On the index action of the > > > articlescontroller you simply want to spec out that it'll scope the results > > > to the ownership of the current_user. > > > > > > It should NOT include any articles other than the articles that user > > > owns. > > > > > > How would you properly spec this out? > > > > > > Thanks for the help! > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > -- > > Bekk Open Source > > http://boss.bekk.no > > _______________________________________________ > > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/06371128/attachment.html From me at danielfischer.com Tue Dec 4 00:25:07 2007 From: me at danielfischer.com (Fischer, Daniel) Date: Mon, 3 Dec 2007 21:25:07 -0800 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> Message-ID: <7e565b5c0712032125n20aba5f2x81a72898dadd227a@mail.gmail.com> I also have another problem, when I am trying to do the similar strategy for XML based speccing it fails on saying nil.to_xml Arg.. And all it is, is the following => (with a before_filter on login required) but still, it's just current_user. def show @writing = current_user.writings.find(params[:id]) respond_to do |format| format.html format.xml { render :xml => @writing.to_xml } end end http://pastie.textmate.org/private/99vq9ipqb6u8cu5bfirlaa here is the pastie. On Dec 3, 2007 8:26 PM, Fischer, Daniel wrote: > Hey cool, thanks for the help guys. One problem though, when I take this > approach I can't decouple the specs anymore. They all "User_xxx receive > unexpected message :articles". It seems silly to include all behaviors in > one spec, or put that expectation in each test. Is there a way around this? > Thanks for all the help, > Daniel Fischer > http://www.danielfischer.com > > > On Dec 3, 2007 2:44 AM, Daniel N < has.sox at gmail.com> wrote: > > > Assuming that there is a call like this in your controller > > @articles = current_user.articles > > > > One way to do this is to stub out the controller.current_user to return > > a mock object of the current_user > > > > Then put an expectation on the current user that it's articles method > > gets called. (return a mocked collection of articles) > > > > Then check that @articles is set to the returned mocked collection of > > articles from current_user.articles > > > > phew... > > > > Ok So one way you might write this could be (This is untested...) > > > > it "should scope the articles to the currrent_user" do > > > > user = mock_model(User) > > articles = [mock_model(Article)] > > > > controller.stub!(:current_user).and_return(user) > > user.should_receive (:articles).and_return(articles) > > > > get :index > > > > assigns[:articles].should == articles > > > > end > > > > Like I said though, that's not tested itself. If that's not exactly > > right... it's along the right track of an option that can work. > > > > HTH > > Daniel > > > > On Dec 3, 2007 9:07 PM, Stefan Magnus Landr? > > wrote: > > > > > Typically, I'd write a method in your user model that returns the > > > user's articles: > > > > > > class User do > > > > > > def find_articles_for_user > > > Article.find(:all, :conditions => ['userid = ?', id) > > > end > > > > > > end > > > > > > Then you'd use a mock in your controller spec, and make sure you test > > > that your method is being called. > > > > > > On the other hand, the user model should be tested directly against > > > the db. > > > > > > HTH, > > > > > > Stefan > > > > > > 2007/12/3, Fischer, Daniel : > > > > > > > > Let's say you're using the restful_authentication plugin. > > > > You have a model called articles. On the index action of the > > > > articlescontroller you simply want to spec out that it'll scope the results > > > > to the ownership of the current_user. > > > > > > > > It should NOT include any articles other than the articles that user > > > > owns. > > > > > > > > How would you properly spec this out? > > > > > > > > Thanks for the help! > > > > > > > > _______________________________________________ > > > > rspec-users mailing list > > > > rspec-users at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > -- > > > Bekk Open Source > > > http://boss.bekk.no > > > _______________________________________________ > > > 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 > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/85856ef7/attachment.html From dchelimsky at gmail.com Tue Dec 4 00:33:58 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 3 Dec 2007 23:33:58 -0600 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> Message-ID: <57c63afe0712032133j2fd73555t235b126c0679eb77@mail.gmail.com> On Dec 3, 2007 10:26 PM, Fischer, Daniel wrote: > Hey cool, thanks for the help guys. One problem though, when I take this > approach I can't decouple the specs anymore. They all "User_xxx receive > unexpected message :articles". It seems silly to include all behaviors in > one spec, or put that expectation in each test. Is there a way around this? Sure. Create a method somewhere that generates a baseline stub for you. module UserControllerSpecHelper def create_stub_user mock_model(User, :articles => []) end end describe UserController, "..." do include UserControllerSpecHelper before(:each) do @user = create_stub_user end end If there are other methods you need to stub on all instances of user, do it in the helper. Then you can use message expectations (mocks) where you need them to describe specific behaviour. HTH, David > > Thanks for all the help, > Daniel Fischer > http://www.danielfischer.com > > > > On Dec 3, 2007 2:44 AM, Daniel N < has.sox at gmail.com> wrote: > > Assuming that there is a call like this in your controller > > > > > > @articles = current_user.articles > > > > > > One way to do this is to stub out the controller.current_user to return a > mock object of the current_user > > > > > > Then put an expectation on the current user that it's articles method gets > called. (return a mocked collection of articles) > > > > > > Then check that @articles is set to the returned mocked collection of > articles from current_user.articles > > > > > > phew... > > > > > > Ok So one way you might write this could be (This is untested...) > > > > > > it "should scope the articles to the currrent_user" do > > > > > > user = mock_model(User) > > articles = [mock_model(Article)] > > > > > > controller.stub!(:current_user).and_return(user) > > > > user.should_receive (:articles).and_return(articles) > > > > get :index > > > > assigns[:articles].should == articles > > > > > > end > > > > > > Like I said though, that's not tested itself. If that's not exactly > right... it's along the right track of an option that can work. > > > > > > HTH > > Daniel > > > > > > > > On Dec 3, 2007 9:07 PM, Stefan Magnus Landr? > wrote: > > > > > > > > > > > Typically, I'd write a method in your user model that returns the user's > articles: > > > > > > class User do > > > > > > def find_articles_for_user > > > Article.find(:all, :conditions => ['userid = ?', id) > > > end > > > > > > end > > > > > > Then you'd use a mock in your controller spec, and make sure you test > that your method is being called. > > > > > > On the other hand, the user model should be tested directly against the > db. > > > > > > HTH, > > > > > > Stefan > > > > > > > > > 2007/12/3, Fischer, Daniel : > > > > > > > > > > > > > > > > Let's say you're using the restful_authentication plugin. > > > > > > > > > > > > You have a model called articles. On the index action of the > articlescontroller you simply want to spec out that it'll scope the results > to the ownership of the current_user. > > > > > > > > > > > > It should NOT include any articles other than the articles that user > owns. > > > > > > > > > > > > How would you properly spec this out? > > > > > > > > > > > > Thanks for the help! > > > > _______________________________________________ > > > > rspec-users mailing list > > > > rspec-users at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > -- > > > Bekk Open Source > > > http://boss.bekk.no > > > _______________________________________________ > > > 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 > From jonathan at parkerhill.com Tue Dec 4 00:41:26 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Tue, 4 Dec 2007 00:41:26 -0500 Subject: [rspec-users] spec'ing shared controller methods Message-ID: <50DA2DB3-5898-4A7F-A8DD-62FC0E93A2F4@parkerhill.com> I want to isolate and spec methods that are shared by controllers, and live in application.rb. Whereas I usually also provide examples in individual controllers that use these methods, not necessarily all the edge cases and I'd like to isolate the examples. This is the approach I'm taking (thanks to bryanl for suggestions and http://pastie.caboo.se/123626). WDYT? This example is a before_filter that finds the Account specified in params[:account_id], which may be used by any number of other controllers in my app. As you can see, the filter is still getting spec'd and has pending examples. ## application.rb class ApplicationController < ActionController::Base ... def find_account @account = Account.find_by_id( params[:account_id]) # TODO: check account status is suspended, closed, different flash message? return( error_redirect_gracefully( "Account not found")) unless @account end ... end ## application_controller_spec.rb require File.dirname(__FILE__) + '/../spec_helper' ActionController::Routing::Routes.draw do |map| map.connect 'foos/:action', :controller => 'foos' end class FoosController < ApplicationController end describe FoosController, "account authorization" do before do FoosController.class_eval do define_method(:foo) { find_account # this is the method being tested } end end it "should find_account from params" do acnt = mock_model(Account) Account.should_receive(:find_by_id).with('99').and_return(acnt) get :foo, :account_id => "99" assigns[:account].should == acnt end it "should redirect gracefully if no account specified" do controller.stub!(:error_redirect_gracefully) controller.should_receive(:error_redirect_gracefully).with ( "Account not found") acnt = mock_model(Account) Account.should_receive(:find_by_id).with(nil).and_return(nil) get :foo end it "should not find accounts that are suspended" it "should flash different message for accounts that are suspended" it "should not find accounts that are closed" end From jonathan at parkerhill.com Tue Dec 4 01:12:57 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Tue, 4 Dec 2007 01:12:57 -0500 Subject: [rspec-users] spec'ing shared controller methods In-Reply-To: <50DA2DB3-5898-4A7F-A8DD-62FC0E93A2F4@parkerhill.com> References: <50DA2DB3-5898-4A7F-A8DD-62FC0E93A2F4@parkerhill.com> Message-ID: <51A484BE-E9B5-43F4-8984-C55A43204C88@parkerhill.com> didnt mean to confuse, i'm asking about the code in the body of the email. The pastie was just for background reference. --J On Dec 4, 2007, at 12:41 AM, Jonathan Linowes wrote: > I want to isolate and spec methods that are shared by controllers, > and live in application.rb. > > Whereas I usually also provide examples in individual controllers > that use these methods, not necessarily all the edge cases and I'd > like to isolate the examples. > > This is the approach I'm taking (thanks to bryanl for suggestions and > http://pastie.caboo.se/123626). WDYT? > > This example is a before_filter that finds the Account specified in > params[:account_id], which may be used by any number of other > controllers in my app. As you can see, the filter is still getting > spec'd and has pending examples. > > ## application.rb > > class ApplicationController < ActionController::Base > ... > def find_account > @account = Account.find_by_id( params[:account_id]) > # TODO: check account status is suspended, closed, different > flash message? > return( error_redirect_gracefully( "Account not found")) unless > @account > end > > ... > end > > ## application_controller_spec.rb > > require File.dirname(__FILE__) + '/../spec_helper' > > ActionController::Routing::Routes.draw do |map| > map.connect 'foos/:action', :controller => 'foos' > end > > class FoosController < ApplicationController > end > > describe FoosController, "account authorization" do > > before do > FoosController.class_eval do > define_method(:foo) { > find_account # this is the method being tested > } > end > end > > it "should find_account from params" do > acnt = mock_model(Account) > Account.should_receive(:find_by_id).with('99').and_return(acnt) > get :foo, :account_id => "99" > assigns[:account].should == acnt > end > > it "should redirect gracefully if no account specified" do > controller.stub!(:error_redirect_gracefully) > controller.should_receive(:error_redirect_gracefully).with > ( "Account not found") > acnt = mock_model(Account) > Account.should_receive(:find_by_id).with(nil).and_return(nil) > get :foo > end > > it "should not find accounts that are suspended" > it "should flash different message for accounts that are suspended" > it "should not find accounts that are closed" > > end > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From me at danielfischer.com Tue Dec 4 01:33:18 2007 From: me at danielfischer.com (Fischer, Daniel) Date: Mon, 3 Dec 2007 22:33:18 -0800 Subject: [rspec-users] Need help mocking this out In-Reply-To: <57c63afe0712032133j2fd73555t235b126c0679eb77@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> <57c63afe0712032133j2fd73555t235b126c0679eb77@mail.gmail.com> Message-ID: <7e565b5c0712032233q6802e613ma823fc6422b39e3a@mail.gmail.com> Alright, thanks, I'm getting more progress in this. As soon as I figure this out I won't have too many problems.. hopefully. Anyone know what "undefined method `call' for "1":String" means? I'm trying to do this: http://pastie.textmate.org/private/17jjjmbave0ph2mkcgp6w On Dec 3, 2007 9:33 PM, David Chelimsky wrote: > On Dec 3, 2007 10:26 PM, Fischer, Daniel wrote: > > Hey cool, thanks for the help guys. One problem though, when I take this > > approach I can't decouple the specs anymore. They all "User_xxx receive > > unexpected message :articles". It seems silly to include all behaviors > in > > one spec, or put that expectation in each test. Is there a way around > this? > > Sure. Create a method somewhere that generates a baseline stub for you. > > module UserControllerSpecHelper > def create_stub_user > mock_model(User, :articles => []) > end > end > > describe UserController, "..." do > include UserControllerSpecHelper > before(:each) do > @user = create_stub_user > end > end > > If there are other methods you need to stub on all instances of user, > do it in the helper. Then you can use message expectations (mocks) > where you need them to describe specific behaviour. > > HTH, > David > > > > > Thanks for all the help, > > Daniel Fischer > > http://www.danielfischer.com > > > > > > > > On Dec 3, 2007 2:44 AM, Daniel N < has.sox at gmail.com> wrote: > > > Assuming that there is a call like this in your controller > > > > > > > > > @articles = current_user.articles > > > > > > > > > One way to do this is to stub out the controller.current_user to > return a > > mock object of the current_user > > > > > > > > > Then put an expectation on the current user that it's articles method > gets > > called. (return a mocked collection of articles) > > > > > > > > > Then check that @articles is set to the returned mocked collection of > > articles from current_user.articles > > > > > > > > > phew... > > > > > > > > > Ok So one way you might write this could be (This is untested...) > > > > > > > > > it "should scope the articles to the currrent_user" do > > > > > > > > > user = mock_model(User) > > > articles = [mock_model(Article)] > > > > > > > > > controller.stub!(:current_user).and_return(user) > > > > > > user.should_receive (:articles).and_return(articles) > > > > > > get :index > > > > > > assigns[:articles].should == articles > > > > > > > > > end > > > > > > > > > Like I said though, that's not tested itself. If that's not exactly > > right... it's along the right track of an option that can work. > > > > > > > > > HTH > > > Daniel > > > > > > > > > > > > On Dec 3, 2007 9:07 PM, Stefan Magnus Landr? > > wrote: > > > > > > > > > > > > > > > > Typically, I'd write a method in your user model that returns the > user's > > articles: > > > > > > > > class User do > > > > > > > > def find_articles_for_user > > > > Article.find(:all, :conditions => ['userid = ?', id) > > > > end > > > > > > > > end > > > > > > > > Then you'd use a mock in your controller spec, and make sure you > test > > that your method is being called. > > > > > > > > On the other hand, the user model should be tested directly against > the > > db. > > > > > > > > HTH, > > > > > > > > Stefan > > > > > > > > > > > > 2007/12/3, Fischer, Daniel : > > > > > > > > > > > > > > > > > > > > Let's say you're using the restful_authentication plugin. > > > > > > > > > > > > > > > You have a model called articles. On the index action of the > > articlescontroller you simply want to spec out that it'll scope the > results > > to the ownership of the current_user. > > > > > > > > > > > > > > > It should NOT include any articles other than the articles that > user > > owns. > > > > > > > > > > > > > > > How would you properly spec this out? > > > > > > > > > > > > > > > Thanks for the help! > > > > > _______________________________________________ > > > > > rspec-users mailing list > > > > > rspec-users at rubyforge.org > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > > > -- > > > > Bekk Open Source > > > > http://boss.bekk.no > > > > _______________________________________________ > > > > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/ced91e2a/attachment.html From me at danielfischer.com Tue Dec 4 01:40:55 2007 From: me at danielfischer.com (Fischer, Daniel) Date: Mon, 3 Dec 2007 22:40:55 -0800 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712032233q6802e613ma823fc6422b39e3a@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> <57c63afe0712032133j2fd73555t235b126c0679eb77@mail.gmail.com> <7e565b5c0712032233q6802e613ma823fc6422b39e3a@mail.gmail.com> Message-ID: <7e565b5c0712032240i45ea4e8ck63cb6b0f74e32051@mail.gmail.com> Sorry for so many messages, I hope I don't get in trouble for this. Maybe IRC would be better if there was a RSpec one. Anyway, the previous problem was solved with the following http://pastie.textmate.org/private/m6qqfd7tzeanw2yar8rua The problem was caused by : @user = mock_model(User, :writings => [] ) I'm not sure if that is a bug or what, but that's what caused it. It'd also say "no block given" if I put something there other than a "mock". On Dec 3, 2007 10:33 PM, Fischer, Daniel wrote: > Alright, thanks, I'm getting more progress in this. As soon as I figure > this out I won't have too many problems.. hopefully. > Anyone know what "undefined method `call' for "1":String" means? > > I'm trying to do this: > > http://pastie.textmate.org/private/17jjjmbave0ph2mkcgp6w > > > On Dec 3, 2007 9:33 PM, David Chelimsky wrote: > > > On Dec 3, 2007 10:26 PM, Fischer, Daniel wrote: > > > Hey cool, thanks for the help guys. One problem though, when I take > > this > > > approach I can't decouple the specs anymore. They all "User_xxx > > receive > > > unexpected message :articles". It seems silly to include all behaviors > > in > > > one spec, or put that expectation in each test. Is there a way around > > this? > > > > Sure. Create a method somewhere that generates a baseline stub for you. > > > > module UserControllerSpecHelper > > def create_stub_user > > mock_model(User, :articles => []) > > end > > end > > > > describe UserController, "..." do > > include UserControllerSpecHelper > > before(:each) do > > @user = create_stub_user > > end > > end > > > > If there are other methods you need to stub on all instances of user, > > do it in the helper. Then you can use message expectations (mocks) > > where you need them to describe specific behaviour. > > > > HTH, > > David > > > > > > > > Thanks for all the help, > > > Daniel Fischer > > > http://www.danielfischer.com > > > > > > > > > > > > On Dec 3, 2007 2:44 AM, Daniel N < has.sox at gmail.com> wrote: > > > > Assuming that there is a call like this in your controller > > > > > > > > > > > > @articles = current_user.articles > > > > > > > > > > > > One way to do this is to stub out the controller.current_user to > > return a > > > mock object of the current_user > > > > > > > > > > > > Then put an expectation on the current user that it's articles > > method gets > > > called. (return a mocked collection of articles) > > > > > > > > > > > > Then check that @articles is set to the returned mocked collection > > of > > > articles from current_user.articles > > > > > > > > > > > > phew... > > > > > > > > > > > > Ok So one way you might write this could be (This is untested...) > > > > > > > > > > > > it "should scope the articles to the currrent_user" do > > > > > > > > > > > > user = mock_model(User) > > > > articles = [mock_model(Article)] > > > > > > > > > > > > controller.stub!(:current_user).and_return(user) > > > > > > > > user.should_receive (:articles).and_return(articles) > > > > > > > > get :index > > > > > > > > assigns[:articles].should == articles > > > > > > > > > > > > end > > > > > > > > > > > > Like I said though, that's not tested itself. If that's not exactly > > > right... it's along the right track of an option that can work. > > > > > > > > > > > > HTH > > > > Daniel > > > > > > > > > > > > > > > > On Dec 3, 2007 9:07 PM, Stefan Magnus Landr? > > > > > wrote: > > > > > > > > > > > > > > > > > > > > > Typically, I'd write a method in your user model that returns the > > user's > > > articles: > > > > > > > > > > class User do > > > > > > > > > > def find_articles_for_user > > > > > Article.find(:all, :conditions => ['userid = ?', id) > > > > > end > > > > > > > > > > end > > > > > > > > > > Then you'd use a mock in your controller spec, and make sure you > > test > > > that your method is being called. > > > > > > > > > > On the other hand, the user model should be tested directly > > against the > > > db. > > > > > > > > > > HTH, > > > > > > > > > > Stefan > > > > > > > > > > > > > > > 2007/12/3, Fischer, Daniel < daniel at helpmebuyacar.org>: > > > > > > > > > > > > > > > > > > > > > > > > Let's say you're using the restful_authentication plugin. > > > > > > > > > > > > > > > > > > You have a model called articles. On the index action of the > > > articlescontroller you simply want to spec out that it'll scope the > > results > > > to the ownership of the current_user. > > > > > > > > > > > > > > > > > > It should NOT include any articles other than the articles that > > user > > > owns. > > > > > > > > > > > > > > > > > > How would you properly spec this out? > > > > > > > > > > > > > > > > > > Thanks for the help! > > > > > > _______________________________________________ > > > > > > rspec-users mailing list > > > > > > rspec-users at rubyforge.org > > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > Bekk Open Source > > > > > http://boss.bekk.no > > > > > _______________________________________________ > > > > > 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 > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/83b97e7b/attachment-0001.html From jarkko at jlaine.net Tue Dec 4 02:40:23 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Tue, 4 Dec 2007 09:40:23 +0200 Subject: [rspec-users] Need help mocking this out In-Reply-To: <7e565b5c0712032240i45ea4e8ck63cb6b0f74e32051@mail.gmail.com> References: <7e565b5c0712030157s4e56b280l1bcf4621c8c8c7a4@mail.gmail.com> <921ca2f80712030207o7f45771dk15006c3ab5b17ab8@mail.gmail.com> <2fff50390712030244t25e59b74y176a4876ba8b011e@mail.gmail.com> <7e565b5c0712032026s7da64526q2faf6758f2d1a6dd@mail.gmail.com> <57c63afe0712032133j2fd73555t235b126c0679eb77@mail.gmail.com> <7e565b5c0712032233q6802e613ma823fc6422b39e3a@mail.gmail.com> <7e565b5c0712032240i45ea4e8ck63cb6b0f74e32051@mail.gmail.com> Message-ID: On 4.12.2007, at 8.40, Fischer, Daniel wrote: > Sorry for so many messages, I hope I don't get in trouble for this. > Maybe IRC would be better if there was a RSpec one. #rspec @freenode //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From hotfusionman at yahoo.com Tue Dec 4 03:17:40 2007 From: hotfusionman at yahoo.com (Al Chou) Date: Tue, 4 Dec 2007 00:17:40 -0800 (PST) Subject: [rspec-users] params not available for controller specs? Message-ID: <493757.46829.qm@web58713.mail.re1.yahoo.com> Hi, all, I'm trying to write a spec for a controller method that starts out: def download @orders = Order.find( params[:ids] ) ... and started writing a spec that set params[:ids] to a mock. I was surprised to discover that controller specs (at least in RSpec 1.0.8) don't offer the use of the params object (per http://rspec.rubyforge.org/documentation/rails/writing/controllers.html), though assigns, flash, and session are available. Maybe I'm missing something, but it seems to me that it would be helpful if controller specs could use params also.... Al ____________________________________________________________________________________ Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/027571ab/attachment.html From jarkko at jlaine.net Tue Dec 4 03:33:11 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Tue, 4 Dec 2007 10:33:11 +0200 Subject: [rspec-users] params not available for controller specs? In-Reply-To: <493757.46829.qm@web58713.mail.re1.yahoo.com> References: <493757.46829.qm@web58713.mail.re1.yahoo.com> Message-ID: On 4.12.2007, at 10.17, Al Chou wrote: > Hi, all, > > I'm trying to write a spec for a controller method that starts out: > > > def download > @orders = Order.find( params[:ids] ) > ... > > and started writing a spec that set params[:ids] to a mock. Why would you want to set params[:ids] to a mock? params values are always basically strings (or hashes/arrays of strings) and you can set them in the actual action call: get :foo, :ids => [1,2,3] Moreover, in your case Order.find should be the thing you want to stub. You don't want the finder call to go to the database, since you're speccing the controller behaviour here, not business logic. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From hotfusionman at yahoo.com Tue Dec 4 04:28:40 2007 From: hotfusionman at yahoo.com (Al Chou) Date: Tue, 4 Dec 2007 01:28:40 -0800 (PST) Subject: [rspec-users] params not available for controller specs? Message-ID: <723651.16277.qm@web58704.mail.re1.yahoo.com> I actually did stub Order.find() but was getting a nil object error because params[:ids] was nil. I can't write controller.download :ids => '1/2/3' in the controller spec, and get download, :ids => ids_string results in the following error message: NameError in 'Admin::OrdersController should split the params[:ids] string to create an array of id's to find for downloading' undefined local variable or method `download' for [RSpec example]:# ./spec/controllers/admin/orders_controller_spec.rb:14: Al ----- Original Message ---- From: Jarkko Laine To: rspec-users Sent: Tuesday, December 4, 2007 12:33:11 AM Subject: Re: [rspec-users] params not available for controller specs? On 4.12.2007, at 10.17, Al Chou wrote: > Hi, all, > > I'm trying to write a spec for a controller method that starts out: > > > def download > @orders = Order.find( params[:ids] ) > ... > > and started writing a spec that set params[:ids] to a mock. Why would you want to set params[:ids] to a mock? params values are always basically strings (or hashes/arrays of strings) and you can set them in the actual action call: get :foo, :ids => [1,2,3] Moreover, in your case Order.find should be the thing you want to stub. You don't want the finder call to go to the database, since you're speccing the controller behaviour here, not business logic. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi _______________________________________________ rspec-users mailing list rspec-users at rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/4ad4f71a/attachment.html From daniel.ruby at tenner.org Tue Dec 4 04:56:25 2007 From: daniel.ruby at tenner.org (Daniel Tenner) Date: Tue, 4 Dec 2007 09:56:25 +0000 Subject: [rspec-users] params not available for controller specs? In-Reply-To: <723651.16277.qm@web58704.mail.re1.yahoo.com> References: <723651.16277.qm@web58704.mail.re1.yahoo.com> Message-ID: <8F81CD13-EA40-48D7-9766-E0FC880D090D@tenner.org> Try: get :download, :ids => ids_string You need the ":" in front of the action name. Daniel On 4 Dec 2007, at 09:28 4 Dec 2007, Al Chou wrote: > I actually did stub Order.find() but was getting a nil object error > because params[:ids] was nil. I can't write > > controller.download :ids => '1/2/3' > > in the controller spec, and > > get download, :ids => ids_string > > results in the following error message: > > NameError in 'Admin::OrdersController should split the params[:ids] > string to create an array of id's to find for downloading' > undefined local variable or method `download' for [RSpec > example]:# > ./spec/controllers/admin/orders_controller_spec.rb:14: > > > Al > > ----- Original Message ---- > From: Jarkko Laine > To: rspec-users > Sent: Tuesday, December 4, 2007 12:33:11 AM > Subject: Re: [rspec-users] params not available for controller specs? > > > On 4.12.2007, at 10.17, Al Chou wrote: > > > Hi, all, > > > > I'm trying to write a spec for a controller method that starts out: > > > > > > def download > > @orders = Order.find( params[:ids] ) > > ... > > > > and started writing a spec that set params[:ids] to a mock. > > Why would you want to set params[:ids] to a mock? params values are > always basically strings (or hashes/arrays of strings) and you can set > them in the actual action call: > > get :foo, :ids => [1,2,3] > > Moreover, in your case Order.find should be the thing you want to > stub. You don't want the finder call to go to the database, since > you're speccing the controller behaviour here, not business logic. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > Never miss a thing. Make Yahoo your homepage. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/49a1610a/attachment.html From hotfusionman at yahoo.com Tue Dec 4 10:13:14 2007 From: hotfusionman at yahoo.com (Al Chou) Date: Tue, 4 Dec 2007 07:13:14 -0800 (PST) Subject: [rspec-users] params not available for controller specs? Message-ID: <168417.96403.qm@web58715.mail.re1.yahoo.com> Ah, thanks! That was a breakthrough. My mock object "ids_string" isn't receiving the method calls I expect (even though the code under test actually does what I want it to do and works correctly), but at least I'm past the params issue. Al ----- Original Message ---- From: Daniel Tenner To: rspec-users Sent: Tuesday, December 4, 2007 1:56:25 AM Subject: Re: [rspec-users] params not available for controller specs? Try: get :download, :ids => ids_string You need the ":" in front of the action name. Daniel On 4 Dec 2007, at 09:28 4 Dec 2007, Al Chou wrote: I actually did stub Order.find() but was getting a nil object error because params[:ids] was nil. I can't write controller.download :ids => '1/2/3' in the controller spec, and get download, :ids => ids_string results in the following error message: NameError in 'Admin::OrdersController should split the params[:ids] string to create an array of id's to find for downloading' undefined local variable or method `download' for [RSpec example]:# ./spec/controllers/admin/orders_controller_spec.rb:14: Al ----- Original Message ---- From: Jarkko Laine To: rspec-users Sent: Tuesday, December 4, 2007 12:33:11 AM Subject: Re: [rspec-users] params not available for controller specs? On 4.12.2007, at 10.17, Al Chou wrote: > Hi, all, > > I'm trying to write a spec for a controller method that starts out: > > > def download > @orders = Order.find( params[:ids] ) > ... > > and started writing a spec that set params[:ids] to a mock. Why would you want to set params[:ids] to a mock? params values are always basically strings (or hashes/arrays of strings) and you can set them in the actual action call: get :foo, :ids => [1,2,3] Moreover, in your case Order.find should be the thing you want to stub. You don't want the finder call to go to the database, since you're speccing the controller behaviour here, not business logic. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi _______________________________________________ rspec-users mailing list rspec-users at rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users ____________________________________________________________________________________ Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/68c50619/attachment-0001.html From jarkko at jlaine.net Tue Dec 4 10:31:11 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Tue, 4 Dec 2007 17:31:11 +0200 Subject: [rspec-users] params not available for controller specs? In-Reply-To: <168417.96403.qm@web58715.mail.re1.yahoo.com> References: <168417.96403.qm@web58715.mail.re1.yahoo.com> Message-ID: On 4.12.2007, at 17.13, Al Chou wrote: > Ah, thanks! That was a breakthrough. My mock object "ids_string" > isn't receiving the method calls I expect (even though the code > under test actually does what I want it to do and works correctly), > but at least I'm past the params issue. You don't get the same object through the action queue, which is another reason for not mocking something there. "string 1" is not necessarily the same object as "string 1" and Rails will do a lot of processing to the parameters before they end up in the params hash. Just assume that a similar string will get passed through and spec that the behaviour is what you expect. What kind of method calls are you expecting/stubbing for a string? //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From andy at adveho.net Tue Dec 4 10:41:40 2007 From: andy at adveho.net (Andy Goundry) Date: Tue, 4 Dec 2007 15:41:40 +0000 Subject: [rspec-users] Newbie question Message-ID: <139c56460712040741rce7a10eic06704bf71543351@mail.gmail.com> Good day all :-) I am getting into RSpec and am a little confused by an aspect of mocks / mock_models in controller tests. I've gone through the online docs and the PeepCode movies and have them slightly contradictory on this matter. I hope you can clarify. Many thanks in advance :-) I'm writing a system that uses the restful authentication plugin and am writing a test for the following controller create method: class AccountsController < ApplicationController def create @account = Account.new(params[:account]) if @account.save! self.current_account = @account redirect_back_or_default('/') flash[:notice] = "Thanks for signing up!" end rescue ActiveRecord::RecordInvalid render :action => 'new' end ... end I've created the following spec test to simply ensure that the Account model receives a call to :new, but the spec is failing as it also receives a call to :save! and it isn't expecting it. I am using "@account = mock_model(Account)". If i replace this with "@account = mock('account', :null_object => true)" the test passes, but i'm not sure that's really what i want. I'm pretty sure i need to us mock_model in this instance. The code is below. Also, are my use of comment blocks within the spec against the RSpec *way*. I appreciate that this isn't using fixtures and i might use them later. I also appreciate that i can write modules to contain these methods, but i can't quite see the point in muddying things. Comments most welcome. Thanks describe AccountsController, "allows users to create only valid accounts" do ####### ### Reusable methods ####### def do_post(params = nil) post :create, :account => params end def valid_account_form_details { :password => "password", :password_confirmation => "password", :login => "new_login", :email => "test at test.com" } end ####### ### Before block ####### before do @account = mock_model(Account) Account.stub!(:new).and_return(@account) end ####### ### It Statements ####### it "should display the new account template when the user visits /accounts/new" do get 'new' response.should render_template(:new) end it "should tell the Account model to create a new account on form POST" do Account.should_receive(:new).with(:anything).and_return(@account) do_post end -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/151ae095/attachment.html From tom at experthuman.com Tue Dec 4 10:58:03 2007 From: tom at experthuman.com (Tom Stuart) Date: Tue, 4 Dec 2007 15:58:03 +0000 Subject: [rspec-users] Newbie question In-Reply-To: <139c56460712040741rce7a10eic06704bf71543351@mail.gmail.com> References: <139c56460712040741rce7a10eic06704bf71543351@mail.gmail.com> Message-ID: On 4 Dec 2007, at 15:41, Andy Goundry wrote: > I've created the following spec test to simply ensure that the > Account model receives a call to :new, but the spec is failing as it > also receives a call to :save! and it isn't expecting it. I am using > "@account = mock_model(Account)". If i replace this with "@account = > mock('account', :null_object => true)" the test passes, but i'm not > sure that's really what i want. I'm pretty sure i need to us > mock_model in this instance. mock_model doesn't do anything magic -- it just creates a mock (with a conveniently-chosen name) and stubs out a few useful methods for you. In your case you need to stub out #save! too, so that you don't get the failure: @account = mock_model(Account, :save! => true). Cheers, -Tom From andy at adveho.net Tue Dec 4 11:06:23 2007 From: andy at adveho.net (Andy Goundry) Date: Tue, 4 Dec 2007 16:06:23 +0000 Subject: [rspec-users] Newbie question In-Reply-To: References: <139c56460712040741rce7a10eic06704bf71543351@mail.gmail.com> Message-ID: <139c56460712040806v18475da9p42ab6de278feefcb@mail.gmail.com> cheers Tom. That worked. I'll go dig in the rspec source... On 04/12/2007, Tom Stuart wrote: > > On 4 Dec 2007, at 15:41, Andy Goundry wrote: > > I've created the following spec test to simply ensure that the > > Account model receives a call to :new, but the spec is failing as it > > also receives a call to :save! and it isn't expecting it. I am using > > "@account = mock_model(Account)". If i replace this with "@account = > > mock('account', :null_object => true)" the test passes, but i'm not > > sure that's really what i want. I'm pretty sure i need to us > > mock_model in this instance. > > mock_model doesn't do anything magic -- it just creates a mock (with a > conveniently-chosen name) and stubs out a few useful methods for you. > In your case you need to stub out #save! too, so that you don't get > the failure: @account = mock_model(Account, :save! => true). > > Cheers, > -Tom > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/2e0862a9/attachment.html From hotfusionman at yahoo.com Tue Dec 4 11:19:04 2007 From: hotfusionman at yahoo.com (Al Chou) Date: Tue, 4 Dec 2007 08:19:04 -0800 (PST) Subject: [rspec-users] params not available for controller specs? Message-ID: <472037.76178.qm@web58711.mail.re1.yahoo.com> Going back to my original message, I have the following at the beginning of the download method: def download @orders = Order.find( params[:ids] ) ... The problem is that params[:ids], although built as an Array object by the view that calls the download method on the controller, is passed as a /-delimited string of id values. Order.find() will not do the desired thing with that string, which was intended to be an array by the view, but Rails passed it in a string representation. So the method really should be def download @orders = Order.find( params[:ids].split( '/' ) ) and what I'm trying to spec is the addition of the call to split(). Al ----- Original Message ---- From: Jarkko Laine To: rspec-users Sent: Tuesday, December 4, 2007 7:31:11 AM Subject: Re: [rspec-users] params not available for controller specs? On 4.12.2007, at 17.13, Al Chou wrote: > Ah, thanks! That was a breakthrough. My mock object "ids_string" > isn't receiving the method calls I expect (even though the code > under test actually does what I want it to do and works correctly), > but at least I'm past the params issue. You don't get the same object through the action queue, which is another reason for not mocking something there. "string 1" is not necessarily the same object as "string 1" and Rails will do a lot of processing to the parameters before they end up in the params hash. Just assume that a similar string will get passed through and spec that the behaviour is what you expect. What kind of method calls are you expecting/stubbing for a string? //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi _______________________________________________ rspec-users mailing list rspec-users at rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/dab6cd6b/attachment.html From jarkko at jlaine.net Tue Dec 4 13:09:01 2007 From: jarkko at jlaine.net (Jarkko Laine) Date: Tue, 4 Dec 2007 20:09:01 +0200 Subject: [rspec-users] params not available for controller specs? In-Reply-To: <472037.76178.qm@web58711.mail.re1.yahoo.com> References: <472037.76178.qm@web58711.mail.re1.yahoo.com> Message-ID: <9A7C4BDD-2788-4F18-946D-7247BE306C13@jlaine.net> On 4.12.2007, at 18.19, Al Chou wrote: > Going back to my original message, I have the following at the > beginning of the download method: > > def download > @orders = Order.find( params[:ids] ) > ... > > The problem is that params[:ids], although built as an Array object > by the view that calls the download method on the controller, is > passed as a /-delimited string of id values. If I set <%= link_to "Test", :controller => "clients", :foo => [1, 2, 3, 4, 5] %> I get this as the url: http://localhost:3000/en/clients?foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3&foo%5B%5D=4&foo%5B%5D=5 That will get correctly parsed back to an array in the receiving action: Parameters: {"action"=>"index", "foo"=>["1", "2", "3", "4", "5"], "controller"=>"clients", "locale"=>"en"} This is Edge Rails, though, so YMMV. > Order.find() will not do the desired thing with that string, which > was intended to be an array by the view, but Rails passed it in a > string representation. So the method really should be > > def download > @orders = Order.find( params[:ids].split( '/' ) ) > > and what I'm trying to spec is the addition of the call to split(). IMHO there's no need to stub string methods like that. Just do something like this: Order.should_receive(:find).with(%w(1 2 3)).and_return(@some_order_objects) If params[:ids] is "1/2/3", you can be pretty certain that split("/") will work correctly so you're more interested in that the find method gets called with correct set of attributes, an array. It's not really interesting how that array got to be. In this case, you might notice that somehow params[:ids] is an array after all and you can take the split call away, and the spec will still pass. I guess what I'm trying to say is that the split call is part of the controller action's internal implementation, and you should be spec'ing how it behaves related to its surrounding world: fetches stuff from the business model, assigns objects for the view etc. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi From schleg at gmail.com Tue Dec 4 12:49:55 2007 From: schleg at gmail.com (schleg) Date: Tue, 4 Dec 2007 09:49:55 -0800 (PST) Subject: [rspec-users] Unexpected message on :attr_accessor Message-ID: <14155614.post@talk.nabble.com> This may be a dumb noob issue, but I haven't found any answers while seaching the forum-- I have a controller method def edit @user = User.find params[:id] @user.password_confirmation = @user.password end The User class has an "attr_accessor :password_confirmation" definition (so "password_confirmation" doesn't exist in the users table). My spec has the following it "should find User on GET to users/edit/:id" do User.should_receive(:find).and_return(@user) @user.should_receive(:password_confirmation) get 'edit', :id => @user.id end I am asking it to expect that I will be assigning something to that attribute in the "edit" method. Unfortunately, the spec fails with Spec::Mocks::MockExpectationError in 'UsersController should find User on GET to users/edit/:id' Mock 'user' received unexpected message :password_confirmation= with ("password") Initially I thought that my "should_receive" expectation was incorrectly written, but if I comment out the attribute assignment in the controller method ... @user = User.find params[:id] #@user.password_confirmation = @user.password ... then the test fails with Spec::Mocks::MockExpectationError in 'UsersController should find User on GET to users/edit/:id' Mock 'user' expected :password_confirmation with (any args) once, but received it 0 times So, it seems to me that the expectation is written correctly, but something about using the attr_accessor via the mock object is causing a failure. BTW, I am doing the following in a before(:each) block @user = mock("user") @user.stub!(:new_record?).and_return(false) @user.stub!(:update_attributes).and_return(true) @user.stub!(:password_confirmation).and_return('password') @user.stub!(:password).and_return('password') User.stub!(:new).and_return(@user) Does anyone know what I'm missing here? Thanks! -- View this message in context: http://www.nabble.com/Unexpected-message-on-%3Aattr_accessor-tf4944588.html#a14155614 Sent from the rspec-users mailing list archive at Nabble.com. From dchelimsky at gmail.com Tue Dec 4 14:02:46 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 4 Dec 2007 13:02:46 -0600 Subject: [rspec-users] Unexpected message on :attr_accessor In-Reply-To: <14155614.post@talk.nabble.com> References: <14155614.post@talk.nabble.com> Message-ID: <57c63afe0712041102n1c2a4554y1bf13308b8924a78@mail.gmail.com> On Dec 4, 2007 11:49 AM, schleg wrote: > > This may be a dumb noob issue, but I haven't found any answers while seaching > the forum-- > > I have a controller method > > def edit > @user = User.find params[:id] > @user.password_confirmation = @user.password > end > > The User class has an "attr_accessor :password_confirmation" definition (so > "password_confirmation" doesn't exist in the users table). My spec has the > following > > it "should find User on GET to users/edit/:id" do > User.should_receive(:find).and_return(@user) > @user.should_receive(:password_confirmation) This should be: @user.should_receive(:password_confirmation=) Cheers, David > get 'edit', :id => @user.id > end > > I am asking it to expect that I will be assigning something to that > attribute in the "edit" method. Unfortunately, the spec fails with > > Spec::Mocks::MockExpectationError in 'UsersController should find User on > GET to users/edit/:id' > Mock 'user' received unexpected message :password_confirmation= with > ("password") > > Initially I thought that my "should_receive" expectation was incorrectly > written, but if I comment out the attribute assignment in the controller > method > > ... > @user = User.find params[:id] > #@user.password_confirmation = @user.password > ... > > then the test fails with > > Spec::Mocks::MockExpectationError in 'UsersController should find User on > GET to users/edit/:id' > Mock 'user' expected :password_confirmation with (any args) once, but > received it 0 times > > So, it seems to me that the expectation is written correctly, but something > about using the attr_accessor via the mock object is causing a failure. > > BTW, I am doing the following in a before(:each) block > > @user = mock("user") > @user.stub!(:new_record?).and_return(false) > @user.stub!(:update_attributes).and_return(true) > @user.stub!(:password_confirmation).and_return('password') > @user.stub!(:password).and_return('password') > User.stub!(:new).and_return(@user) > > Does anyone know what I'm missing here? > > Thanks! > -- > View this message in context: http://www.nabble.com/Unexpected-message-on-%3Aattr_accessor-tf4944588.html#a14155614 > 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 > From aslak.hellesoy at gmail.com Tue Dec 4 14:27:39 2007 From: aslak.hellesoy at gmail.com (aslak hellesoy) Date: Tue, 4 Dec 2007 20:27:39 +0100 Subject: [rspec-users] Unexpected message on :attr_accessor In-Reply-To: <57c63afe0712041102n1c2a4554y1bf13308b8924a78@mail.gmail.com> References: <14155614.post@talk.nabble.com> <57c63afe0712041102n1c2a4554y1bf13308b8924a78@mail.gmail.com> Message-ID: <8d961d900712041127jb60e417w558daf867fc5a87f@mail.gmail.com> On Dec 4, 2007 8:02 PM, David Chelimsky wrote: > On Dec 4, 2007 11:49 AM, schleg wrote: > > > > This may be a dumb noob issue, but I haven't found any answers while seaching > > the forum-- > > > > I have a controller method > > > > def edit > > @user = User.find params[:id] > > @user.password_confirmation = @user.password > > end > > > > The User class has an "attr_accessor :password_confirmation" definition (so > > "password_confirmation" doesn't exist in the users table). My spec has the > > following > > > > it "should find User on GET to users/edit/:id" do > > User.should_receive(:find).and_return(@user) > > @user.should_receive(:password_confirmation) > > This should be: > > @user.should_receive(:password_confirmation=) > Lots of beginners make this mistake. Maybe RSpec's mock framework should be smart enough to suggest this fix by itself. Patch anyone? Aslak > Cheers, > David > > > > get 'edit', :id => @user.id > > end > > > > I am asking it to expect that I will be assigning something to that > > attribute in the "edit" method. Unfortunately, the spec fails with > > > > Spec::Mocks::MockExpectationError in 'UsersController should find User on > > GET to users/edit/:id' > > Mock 'user' received unexpected message :password_confirmation= with > > ("password") > > > > Initially I thought that my "should_receive" expectation was incorrectly > > written, but if I comment out the attribute assignment in the controller > > method > > > > ... > > @user = User.find params[:id] > > #@user.password_confirmation = @user.password > > ... > > > > then the test fails with > > > > Spec::Mocks::MockExpectationError in 'UsersController should find User on > > GET to users/edit/:id' > > Mock 'user' expected :password_confirmation with (any args) once, but > > received it 0 times > > > > So, it seems to me that the expectation is written correctly, but something > > about using the attr_accessor via the mock object is causing a failure. > > > > BTW, I am doing the following in a before(:each) block > > > > @user = mock("user") > > @user.stub!(:new_record?).and_return(false) > > @user.stub!(:update_attributes).and_return(true) > > @user.stub!(:password_confirmation).and_return('password') > > @user.stub!(:password).and_return('password') > > User.stub!(:new).and_return(@user) > > > > Does anyone know what I'm missing here? > > > > Thanks! > > -- > > View this message in context: http://www.nabble.com/Unexpected-message-on-%3Aattr_accessor-tf4944588.html#a14155614 > > 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 > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From hotfusionman at yahoo.com Tue Dec 4 15:09:42 2007 From: hotfusionman at yahoo.com (Al Chou) Date: Tue, 4 Dec 2007 12:09:42 -0800 (PST) Subject: [rspec-users] params not available for controller specs? Message-ID: <606252.81870.qm@web58704.mail.re1.yahoo.com> I get what you're saying, but I was trying to fix a bug in existing code in Substruct (that I did not write) that was caused by Rails passing the array as a /-delimited string and then not automatically decoding that string. As Substruct does not say that Edge Rails is a requirement, I felt it was worth documenting what I had to change to run it on Rails 1.2.x. Every code change should be driven by a test or example; perhaps in this situation I should've gone over to Test::Unit instead of staying in RSpec? An interesting philosophical thought.... Al ----- Original Message ---- From: Jarkko Laine To: rspec-users Sent: Tuesday, December 4, 2007 10:09:01 AM Subject: Re: [rspec-users] params not available for controller specs? On 4.12.2007, at 18.19, Al Chou wrote: > Going back to my original message, I have the following at the > beginning of the download method: > > def download > @orders = Order.find( params[:ids] ) > ... > > The problem is that params[:ids], although built as an Array object > by the view that calls the download method on the controller, is > passed as a /-delimited string of id values. If I set <%= link_to "Test", :controller => "clients", :foo => [1, 2, 3, 4, 5] %> I get this as the url: http://localhost:3000/en/clients?foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3&foo%5B%5D=4&foo%5B%5D=5 That will get correctly parsed back to an array in the receiving action: Parameters: {"action"=>"index", "foo"=>["1", "2", "3", "4", "5"], "controller"=>"clients", "locale"=>"en"} This is Edge Rails, though, so YMMV. > Order.find() will not do the desired thing with that string, which > was intended to be an array by the view, but Rails passed it in a > string representation. So the method really should be > > def download > @orders = Order.find( params[:ids].split( '/' ) ) > > and what I'm trying to spec is the addition of the call to split(). IMHO there's no need to stub string methods like that. Just do something like this: Order.should_receive(:find).with(%w(1 2 3)).and_return(@some_order_objects) If params[:ids] is "1/2/3", you can be pretty certain that split("/") will work correctly so you're more interested in that the find method gets called with correct set of attributes, an array. It's not really interesting how that array got to be. In this case, you might notice that somehow params[:ids] is an array after all and you can take the split call away, and the spec will still pass. I guess what I'm trying to say is that the split call is part of the controller action's internal implementation, and you should be spec'ing how it behaves related to its surrounding world: fetches stuff from the business model, assigns objects for the view etc. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi _______________________________________________ rspec-users mailing list rspec-users at rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users ____________________________________________________________________________________ Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it now. http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/ee360c0a/attachment.html From lists at ruby-forum.com Tue Dec 4 15:13:44 2007 From: lists at ruby-forum.com (Chris Olsen) Date: Tue, 4 Dec 2007 21:13:44 +0100 Subject: [rspec-users] svn: Connection closed unexpectedly when getting trunk Message-ID: <7c520447b45ff3af42c30b16d62f5825@ruby-forum.com> Is anyone else getting this message? With rails 2.0 pre-releases it looks like you also have to be working with the latest version of rpsec. $ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/trunk/rspec svn: Connection closed unexpectedly The above is what I get when I try to install the trunk version. Is there something that I am missing? Thanks -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Tue Dec 4 15:18:58 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 4 Dec 2007 14:18:58 -0600 Subject: [rspec-users] params not available for controller specs? In-Reply-To: <606252.81870.qm@web58704.mail.re1.yahoo.com> References: <606252.81870.qm@web58704.mail.re1.yahoo.com> Message-ID: <57c63afe0712041218k781b47dgf41766a0c5c4bbb3@mail.gmail.com> On Dec 4, 2007 2:09 PM, Al Chou wrote: > > I get what you're saying, but I was trying to fix a bug in existing code in > Substruct (that I did not write) that was caused by Rails passing the array > as a /-delimited string and then not automatically decoding that string. As > Substruct does not say that Edge Rails is a requirement, I felt it was worth > documenting what I had to change to run it on Rails 1.2.x. Every code > change should be driven by a test or example; perhaps in this situation I > should've gone over to Test::Unit instead of staying in RSpec? An > interesting philosophical thought.... Seems to me this thread has been about how to deal with rails. I don't see what that has to do w/ a T::U vs rspec decision. > > Al > > > > ----- Original Message ---- > From: Jarkko Laine > To: rspec-users > Sent: Tuesday, December 4, 2007 10:09:01 AM > Subject: Re: [rspec-users] params not available for controller specs? > > > On 4.12.2007, at 18.19, Al Chou wrote: > > > Going back to my original message, I have the following at the > > beginning of the download method: > > > > def download > > @orders = Order.find( params[:ids] ) > > ... > > > > The problem is that params[:ids], although built as an Array object > > by the view that calls the download method on the controller, is > > passed as a /-delimited string of id values. > > If I set > > <%= link_to "Test", :controller => "clients", :foo => [1, 2, 3, 4, > 5] %> > > I get this as the url: > > http://localhost:3000/en/clients?foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3&foo%5B%5D=4&foo%5B%5D=5 > > That will get correctly parsed back to an array in the receiving action: > > Parameters: {"action"=>"index", "foo"=>["1", "2", "3", "4", "5"], > "controller"=>"clients", "locale"=>"en"} > > This is Edge Rails, though, so YMMV. > > > Order.find() will not do the desired thing with that string, which > > was intended to be an array by the view, but Rails passed it in a > > string representation. So the method really should be > > > > def download > > @orders = Order.find( params[:ids].split( '/' ) ) > > > > and what I'm trying to spec is the addition of the call to split(). > > IMHO there's no need to stub string methods like that. Just do > something like this: > > Order.should_receive(:find).with(%w(1 2 > 3)).and_return(@some_order_objects) > > If params[:ids] is "1/2/3", you can be pretty certain that split("/") > will work correctly so you're more interested in that the find method > gets called with correct set of attributes, an array. It's not really > interesting how that array got to be. In this case, you might notice > that somehow params[:ids] is an array after all and you can take the > split call away, and the spec will still pass. > > I guess what I'm trying to say is that the split call is part of the > controller action's internal implementation, and you should be > spec'ing how it behaves related to its surrounding world: fetches > stuff from the business model, assigns objects for the view etc. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > ________________________________ > Never miss a thing. Make Yahoo your homepage. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Tue Dec 4 15:20:02 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 4 Dec 2007 14:20:02 -0600 Subject: [rspec-users] svn: Connection closed unexpectedly when getting trunk In-Reply-To: <7c520447b45ff3af42c30b16d62f5825@ruby-forum.com> References: <7c520447b45ff3af42c30b16d62f5825@ruby-forum.com> Message-ID: <57c63afe0712041220hb5282e3i44e844b583c4d434@mail.gmail.com> On Dec 4, 2007 2:13 PM, Chris Olsen wrote: > Is anyone else getting this message? > > With rails 2.0 pre-releases it looks like you also have to be working > with the latest version of rpsec. > > $ ruby script/plugin install > svn://rubyforge.org/var/svn/rspec/trunk/rspec > svn: Connection closed unexpectedly > > The above is what I get when I try to install the trunk version. Try: http://rspec.rubyforge.org/svn/trunk/rspec http://rspec.rubyforge.org/svn/trunk/rspec_on_rails > > Is there something that I am missing? > > Thanks > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Tue Dec 4 15:20:47 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 4 Dec 2007 14:20:47 -0600 Subject: [rspec-users] [ANN] Webrat 0.1.0 released - Ruby Acceptance Testing for Web applications In-Reply-To: References: Message-ID: <57c63afe0712041220o2671e394v193abc8a0b7998bd@mail.gmail.com> On Nov 30, 2007 3:28 PM, Jens-Christian Fischer wrote: > > > > Code is available at: http://svn.eastmedia.net/public/plugins/webrat/ > > > * Rails integration tests in Test::Unit _or_ > > * RSpec stories (using an RSpec version >= revision 2997) > > I had to add: > > require 'cgi' > require "cgi/session" > require 'cgi/session/pstore' > require 'action_controller/cgi_ext/cgi_methods' You could also apply this patch instead: diff -r 80ee2d7fc95d -r 617747be70b6 vendor/plugins/webrat/lib/webrat/session.rb --- a/vendor/plugins/webrat/lib/webrat/session.rb Tue Dec 04 13:53:46 2007 -0600 +++ b/vendor/plugins/webrat/lib/webrat/session.rb Tue Dec 04 14:04:50 2007 -0600 @@ -102,7 +102,12 @@ module ActionController def add_form_data(input_element, value) # :nodoc: form = form_for_node(input_element) - data = CGIMethods::parse_query_parameters("#{input_element.attributes["name"]}=#{value}") + if defined?(CGIMethods) + parser = CGIMethods + else + parser = request.class + end + data = parser.parse_query_parameters("#{input_element.attributes["name"]}=#{value}") merge_form_data(form_number(form), data) end > > in the beginning of lib/webrat/session.rb to get rid of the following > error: > > NameError: uninitialized constant > ActionController::Integration::Session::CGIMethods > /Users/jcf/dev/work/quevita/vendor/rails/activerecord/lib/../../ > activesupport/lib/active_support/dependencies.rb:478:in `const_missing' > /Users/jcf/dev/work/quevita/vendor/plugins/webrat/lib/webrat/ > session.rb:107:in `add_form_data' > /Users/jcf/dev/work/quevita/vendor/plugins/webrat/lib/webrat/ > session.rb:175:in `add_default_params_for' > > > nice work! > > Jens-Christian > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From mailing_lists at railsnewbie.com Tue Dec 4 15:37:38 2007 From: mailing_lists at railsnewbie.com (Scott Taylor) Date: Tue, 4 Dec 2007 15:37:38 -0500 Subject: [rspec-users] svn: Connection closed unexpectedly when getting trunk In-Reply-To: <57c63afe0712041220hb5282e3i44e844b583c4d434@mail.gmail.com> References: <7c520447b45ff3af42c30b16d62f5825@ruby-forum.com> <57c63afe0712041220hb5282e3i44e844b583c4d434@mail.gmail.com> Message-ID: On Dec 4, 2007, at 3:20 PM, David Chelimsky wrote: > On Dec 4, 2007 2:13 PM, Chris Olsen wrote: >> Is anyone else getting this message? >> >> With rails 2.0 pre-releases it looks like you also have to be working >> with the latest version of rpsec. >> >> $ ruby script/plugin install >> svn://rubyforge.org/var/svn/rspec/trunk/rspec >> svn: Connection closed unexpectedly >> >> The above is what I get when I try to install the trunk version. > > Try: > > http://rspec.rubyforge.org/svn/trunk/rspec > http://rspec.rubyforge.org/svn/trunk/rspec_on_rails Yeah - the http connection is a lot more reliable than the svn one. Scott From mlins at webinforem.com Tue Dec 4 15:43:46 2007 From: mlins at webinforem.com (Matthew Lins) Date: Tue, 04 Dec 2007 14:43:46 -0600 Subject: [rspec-users] AutoTest / Rspec - "stack level too deep" Message-ID: Hello, I'm running AutoTest with Rspec on a Rails application. Every 20 or so runs I get "stack level too deep" on one particular controller stub. Ex. controller.stub!(:login).and_return(true) This particular line is in a before block of a certain describe block. The strange thing is, every single spec in this file fails on that one line, even the specs in other describe blocks! Just for the hell of it, I commented it out. Then it fails on another controller stub in another describe block. Same behavior, every spec fails on this one line. I continue commenting the lines that are causing problems. I noticed it only happens with controller stubs and mocks. Once I finally get all controller mocks and stubs commented out, then my spec fails for legitimate reasons (because of the missing stubs). The only way I can fix this, is to reset AutoTest. Any ideas? Thanks, Matt P.S. If you really need to see this 750 line spec file, I'd be glad to pastie it, but I don't think it'll reveal anything. From jonathan at parkerhill.com Tue Dec 4 15:54:43 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Tue, 4 Dec 2007 15:54:43 -0500 Subject: [rspec-users] Unexpected message on :attr_accessor In-Reply-To: <8d961d900712041127jb60e417w558daf867fc5a87f@mail.gmail.com> References: <14155614.post@talk.nabble.com> <57c63afe0712041102n1c2a4554y1bf13308b8924a78@mail.gmail.com> <8d961d900712041127jb60e417w558daf867fc5a87f@mail.gmail.com> Message-ID: <4E57E9BE-2214-4444-9FAB-68667B29052F@parkerhill.com> >> >> This should be: >> >> @user.should_receive(:password_confirmation=) >> > > Lots of beginners make this mistake. Maybe RSpec's mock framework > should be smart enough to suggest this fix by itself. > > Patch anyone? > > Aslak >> perhaps be even more explicit that it's an accessor, like User.should_set(:password_confirmation) User.should_get(:password_confirmation) From smingins at elctech.com Tue Dec 4 15:53:32 2007 From: smingins at elctech.com (Shane Mingins) Date: Wed, 5 Dec 2007 09:53:32 +1300 Subject: [rspec-users] AutoTest / Rspec - "stack level too deep" In-Reply-To: References: Message-ID: <9566E5AF-7791-42DD-9682-8F809FC74764@elctech.com> On 5/12/2007, at 9:43 AM, Matthew Lins wrote: > Hello, > > I'm running AutoTest with Rspec on a Rails application. > > Every 20 or so runs I get "stack level too deep" on one particular > controller stub. > > So this spec fails when you just run it?? I was a little unsure if you are having a problem with autotest or with your spec ... it seemed the later? Cheers Shane From dchelimsky at gmail.com Tue Dec 4 16:00:28 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 4 Dec 2007 15:00:28 -0600 Subject: [rspec-users] Unexpected message on :attr_accessor In-Reply-To: <4E57E9BE-2214-4444-9FAB-68667B29052F@parkerhill.com> References: <14155614.post@talk.nabble.com> <57c63afe0712041102n1c2a4554y1bf13308b8924a78@mail.gmail.com> <8d961d900712041127jb60e417w558daf867fc5a87f@mail.gmail.com> <4E57E9BE-2214-4444-9FAB-68667B29052F@parkerhill.com> Message-ID: <57c63afe0712041300m33b6c0a8jea01407e605fe928@mail.gmail.com> On Dec 4, 2007 2:54 PM, Jonathan Linowes wrote: > > >> > >> This should be: > >> > >> @user.should_receive(:password_confirmation=) > >> > > > > Lots of beginners make this mistake. Maybe RSpec's mock framework > > should be smart enough to suggest this fix by itself. > > > > Patch anyone? > > > > Aslak > >> > > perhaps be even more explicit that it's an accessor, like > > User.should_set(:password_confirmation) > User.should_get(:password_confirmation) This is silly. Mock frameworks are not there to help you learn the language. Adding getter/setter knowledge into a framework is completely backwards. -1000 > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From jonathan at parkerhill.com Tue Dec 4 16:09:34 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Tue, 4 Dec 2007 16:09:34 -0500 Subject: [rspec-users] Unexpected message on :attr_accessor In-Reply-To: <4E57E9BE-2214-4444-9FAB-68667B29052F@parkerhill.com> References: <14155614.post@talk.nabble.com> <57c63afe0712041102n1c2a4554y1bf13308b8924a78@mail.gmail.com> <8d961d900712041127jb60e417w558daf867fc5a87f@mail.gmail.com> <4E57E9BE-2214-4444-9FAB-68667B29052F@parkerhill.com> Message-ID: err, that suggestion was supposed to read @user.should_set... etc On Dec 4, 2007, at 3:54 PM, Jonathan Linowes wrote: > >>> >>> This should be: >>> >>> @user.should_receive(:password_confirmation=) >>> >> >> Lots of beginners make this mistake. Maybe RSpec's mock framework >> should be smart enough to suggest this fix by itself. >> >> Patch anyone? >> >> Aslak >>> > > perhaps be even more explicit that it's an accessor, like > > User.should_set(:password_confirmation) > User.should_get(:password_confirmation) > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From hotfusionman at yahoo.com Tue Dec 4 16:11:43 2007 From: hotfusionman at yahoo.com (Al Chou) Date: Tue, 4 Dec 2007 13:11:43 -0800 (PST) Subject: [rspec-users] params not available for controller specs? Message-ID: <127355.80851.qm@web58701.mail.re1.yahoo.com> I think Jarkko is saying that no spec should have been written for the code I was trying to fix/change. I'm not sure I would agree that BDD/RSpec is an inappropriate tool for documenting what I was trying to change, but I think he would argue that.... Al ----- Original Message ---- From: David Chelimsky To: rspec-users Sent: Tuesday, December 4, 2007 12:18:58 PM Subject: Re: [rspec-users] params not available for controller specs? On Dec 4, 2007 2:09 PM, Al Chou wrote: > > I get what you're saying, but I was trying to fix a bug in existing code in > Substruct (that I did not write) that was caused by Rails passing the array > as a /-delimited string and then not automatically decoding that string. As > Substruct does not say that Edge Rails is a requirement, I felt it was worth > documenting what I had to change to run it on Rails 1.2.x. Every code > change should be driven by a test or example; perhaps in this situation I > should've gone over to Test::Unit instead of staying in RSpec? An > interesting philosophical thought.... Seems to me this thread has been about how to deal with rails. I don't see what that has to do w/ a T::U vs rspec decision. > > Al > > > > ----- Original Message ---- > From: Jarkko Laine > To: rspec-users > Sent: Tuesday, December 4, 2007 10:09:01 AM > Subject: Re: [rspec-users] params not available for controller specs? > > > On 4.12.2007, at 18.19, Al Chou wrote: > > > Going back to my original message, I have the following at the > > beginning of the download method: > > > > def download > > @orders = Order.find( params[:ids] ) > > ... > > > > The problem is that params[:ids], although built as an Array object > > by the view that calls the download method on the controller, is > > passed as a /-delimited string of id values. > > If I set > > <%= link_to "Test", :controller => "clients", :foo => [1, 2, 3, 4, > 5] %> > > I get this as the url: > > http://localhost:3000/en/clients?foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3&foo%5B%5D=4&foo%5B%5D=5 > > That will get correctly parsed back to an array in the receiving action: > > Parameters: {"action"=>"index", "foo"=>["1", "2", "3", "4", "5"], > "controller"=>"clients", "locale"=>"en"} > > This is Edge Rails, though, so YMMV. > > > Order.find() will not do the desired thing with that string, which > > was intended to be an array by the view, but Rails passed it in a > > string representation. So the method really should be > > > > def download > > @orders = Order.find( params[:ids].split( '/' ) ) > > > > and what I'm trying to spec is the addition of the call to split(). > > IMHO there's no need to stub string methods like that. Just do > something like this: > > Order.should_receive(:find).with(%w(1 2 > 3)).and_return(@some_order_objects) > > If params[:ids] is "1/2/3", you can be pretty certain that split("/") > will work correctly so you're more interested in that the find method > gets called with correct set of attributes, an array. It's not really > interesting how that array got to be. In this case, you might notice > that somehow params[:ids] is an array after all and you can take the > split call away, and the spec will still pass. > > I guess what I'm trying to say is that the split call is part of the > controller action's internal implementation, and you should be > spec'ing how it behaves related to its surrounding world: fetches > stuff from the business model, assigns objects for the view etc. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users ____________________________________________________________________________________ Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/6ebf9704/attachment.html From dchelimsky at gmail.com Tue Dec 4 16:18:23 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 4 Dec 2007 15:18:23 -0600 Subject: [rspec-users] Unexpected message on :attr_accessor In-Reply-To: <57c63afe0712041300m33b6c0a8jea01407e605fe928@mail.gmail.com> References: <14155614.post@talk.nabble.com> <57c63afe0712041102n1c2a4554y1bf13308b8924a78@mail.gmail.com> <8d961d900712041127jb60e417w558daf867fc5a87f@mail.gmail.com> <4E57E9BE-2214-4444-9FAB-68667B29052F@parkerhill.com> <57c63afe0712041300m33b6c0a8jea01407e605fe928@mail.gmail.com> Message-ID: <57c63afe0712041318s7dc813f6occed19000bc4ce89@mail.gmail.com> On Dec 4, 2007 3:00 PM, David Chelimsky wrote: > On Dec 4, 2007 2:54 PM, Jonathan Linowes wrote: > > > > >> > > >> This should be: > > >> > > >> @user.should_receive(:password_confirmation=) > > >> > > > > > > Lots of beginners make this mistake. Maybe RSpec's mock framework > > > should be smart enough to suggest this fix by itself. > > > > > > Patch anyone? > > > > > > Aslak > > >> > > > > perhaps be even more explicit that it's an accessor, like > > > > User.should_set(:password_confirmation) > > User.should_get(:password_confirmation) > > This is silly. Mock frameworks are not there to help you learn the > language. That was a knee-jerk reaction. Let me explain. We already have should_receive(:message). :password_confirmation and password_confirmation= are two completely different messages. So saying should_set(:password_confirmation) hides what the message is that you're looking for. RSpec does aim to make things readable and consequently pleasing, but it's not here to protect you from having to learn the language. This is the same issue we had in rspec core w/ should equal(value). There are 4 versions of equal in Ruby and they all have different meanings, so we set up rspec to expose them, which means you have to understand Ruby to understand what "should equal" means. > Adding getter/setter knowledge into a framework is > completely backwards. Just following up on that thought - even though duplication is the root of all evil in OO, so are getters and setters. They act like they are encapsulating something, but really they encourage objects to interact with the state of other objects instead of interacting with the objects themselves and letting the