From pat.maddox at gmail.com Wed Jul 1 03:49:19 2009 From: pat.maddox at gmail.com (Pat Maddox) Date: Wed, 1 Jul 2009 00:49:19 -0700 Subject: [rspec-users] Integrate or isolate views? In-Reply-To: References: Message-ID: <2c7e61990907010049s1ce2b64cq6c90c6c1bb46c7ea@mail.gmail.com> If your controllers are fat, test in isolation. For skinny controllers I will sometimes forgo controller specs altogether and implicitly verify the integration through cucumber features. Sometimes there's something funky that makes the cucumber failure output difficult to interpret, and controller specs can make it easy to pinpoint. Pat On Sun, Jun 28, 2009 at 7:32 AM, Jesse Crockett wrote: > Hello, > > I've been trying for two years to pick up BDD. ?I'm making progress, > have just read through the chapters in The RSpec Book on spec'ing views > and controllers. > > What is the difference between using integrate_views and doing what > seems to be a lot of extra work to test the views in isolation? > > When I use integrate_views, can I write view spec in what would > otherwise be isolated controller spec? > > I read that I'm "encouraged" to do these in isolation, but IMHO the > chapter on spec'ing views is not very convincing in its own right, it > tells me that it's good, but doesn't show me as much, compared to the > examples and descriptions of circumstance that make several other > chapters very convincing. > > Please help. ?thanks > > Jesse > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From lists at ruby-forum.com Wed Jul 1 04:18:55 2009 From: lists at ruby-forum.com (Ignacy Moryc) Date: Wed, 1 Jul 2009 10:18:55 +0200 Subject: [rspec-users] [Rails] Controller testing problem Message-ID: <96354295919f15271ea31829c858a559@ruby-forum.com> Hi, I'm working on a legacy app - it was created in 1.2.5 then over time ported to 2.3. It has absolutely no tests - and I'm trying to fix that in between adding new functionality. My problem right now is that my ApplicationController is running before_filters for setting different request variables (like current location, etc.) and this messes up my tests - I can't test responses properly (they are all redirects because ApplicationController derives from SiteController which actions implement setting variables). What's worse I can't test assigns hash - it doesn't get proper values assigned. Is there a way for RSpec to skip the filters in tests? Or to stop ApplicationController to derive from SiteController? Thanks, Ignacy -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Wed Jul 1 07:48:08 2009 From: lists at ruby-forum.com (Fernando Perez) Date: Wed, 1 Jul 2009 13:48:08 +0200 Subject: [rspec-users] [Rails] Controller testing problem In-Reply-To: <96354295919f15271ea31829c858a559@ruby-forum.com> References: <96354295919f15271ea31829c858a559@ruby-forum.com> Message-ID: <09aeeb4ad35c4ca42879cc7dea03d866@ruby-forum.com> > Is there a way for RSpec to skip the filters in tests? Or to stop > ApplicationController to derive from SiteController? That's a perfect use case for mocks and/or stubs. -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Wed Jul 1 08:43:57 2009 From: lists at ruby-forum.com (Kga Agk) Date: Wed, 1 Jul 2009 14:43:57 +0200 Subject: [rspec-users] Selenium, Rspec in ruby. Start selenium server in code Message-ID: <7964a4684d41af36546cbe903bcfad2c@ruby-forum.com> I got it to work in ruby, but I have to start the selenium servver manualy before I runn the program. That is unesesary mutch work and complicated. Is there a way to start the selenium server inside the code and stop it at the end? Here is a code example i got to work... it is a bit messy require 'rubygems' gem 'thoughtbot-shoulda' require 'shoulda' require 'spec' require 'spec/story' require 'test/unit' require "rubygems" gem "selenium-client", ">=1.2.15" require "selenium/client" require "selenium" describe "test" do before :all do @browser = Selenium::Client::Driver.new \ :host => "localhost", :port => 4444, :browser => "*firefox", :url => "http://www.google.com", :timeout_in_second => 60 end after :all do @browser.close_current_browser_session end it "test1" do @browser.start_new_browser_session @browser.open "/" @browser.type "q", "Selenium seleniumhq.org" @browser.click "btnG", :wait_for => :page assert @browser.text?("seleniumhq.org") end end What do I need to add to get it to start the selenium server automaticly and stop it at the end? -- Posted via http://www.ruby-forum.com/. From matt at mattwynne.net Wed Jul 1 12:55:17 2009 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 1 Jul 2009 17:55:17 +0100 Subject: [rspec-users] [Rails] Controller testing problem In-Reply-To: <09aeeb4ad35c4ca42879cc7dea03d866@ruby-forum.com> References: <96354295919f15271ea31829c858a559@ruby-forum.com> <09aeeb4ad35c4ca42879cc7dea03d866@ruby-forum.com> Message-ID: <63FE445E-5240-4F13-9176-33470E093859@mattwynne.net> On 1 Jul 2009, at 12:48, Fernando Perez wrote: >> Is there a way for RSpec to skip the filters in tests? Or to stop >> ApplicationController to derive from SiteController? > That's a perfect use case for mocks and/or stubs. Indeed. Another way to do this is to test a special subclass of SiteController, which overrides those methods that are called by the before_filters and makes them do what you want for the test (i.e. probably very little). See Michael Feathers' excellent book 'Working Effectively with Legacy Code' for more inspiration and ideas in wrestling with a codebase that has no existing tests. cheers, Matt Wynne http://mattwynne.net +447974 430184 From pat.maddox at gmail.com Wed Jul 1 13:10:46 2009 From: pat.maddox at gmail.com (Pat Maddox) Date: Wed, 1 Jul 2009 10:10:46 -0700 Subject: [rspec-users] Integrate or isolate views? In-Reply-To: <2F96CC14-B30F-48A4-9899-E75E14DB114C@ultrasaurus.com> References: <03369D62-AD08-4922-8D43-8289506E9AAC@benmabey.com> <2F96CC14-B30F-48A4-9899-E75E14DB114C@ultrasaurus.com> Message-ID: <2c7e61990907011010r3e5b6a6fy72ec268143d671fb@mail.gmail.com> On Sun, Jun 28, 2009 at 11:56 AM, Sarah Allen wrote: > I find that testing views independently is useful just to catch HTML errors > that can sometime creep in during a re-factor. ?These check important > details that would be more tedious using cucumber +1 Pat From zach.dennis at gmail.com Wed Jul 1 13:14:37 2009 From: zach.dennis at gmail.com (Zach Dennis) Date: Wed, 1 Jul 2009 13:14:37 -0400 Subject: [rspec-users] Integrate or isolate views? In-Reply-To: <2c7e61990907011010r3e5b6a6fy72ec268143d671fb@mail.gmail.com> References: <03369D62-AD08-4922-8D43-8289506E9AAC@benmabey.com> <2F96CC14-B30F-48A4-9899-E75E14DB114C@ultrasaurus.com> <2c7e61990907011010r3e5b6a6fy72ec268143d671fb@mail.gmail.com> Message-ID: <85d99afe0907011014gd1bc5bcp4f1bb588479544f@mail.gmail.com> On Wed, Jul 1, 2009 at 1:10 PM, Pat Maddox wrote: > On Sun, Jun 28, 2009 at 11:56 AM, Sarah Allen wrote: >> I find that testing views independently is useful just to catch HTML errors >> that can sometime creep in during a re-factor. ?These check important >> details that would be more tedious using cucumber > > +1 +1 as well. The last sentence here really nails it on the head for me, > > Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- Zach Dennis http://www.continuousthinking.com (personal) http://www.mutuallyhuman.com (hire me) http://ideafoundry.info/behavior-driven-development (first rate BDD training) @zachdennis (twitter) From zach.dennis at gmail.com Wed Jul 1 13:14:11 2009 From: zach.dennis at gmail.com (Zach Dennis) Date: Wed, 1 Jul 2009 13:14:11 -0400 Subject: [rspec-users] Integrate or isolate views? In-Reply-To: References: Message-ID: <85d99afe0907011014x76af08e3t7e1cfb06ae011f32@mail.gmail.com> On Sun, Jun 28, 2009 at 10:32 AM, Jesse Crockett wrote: > Hello, > > I've been trying for two years to pick up BDD. ?I'm making progress, > have just read through the chapters in The RSpec Book on spec'ing views > and controllers. > > What is the difference between using integrate_views and doing what > seems to be a lot of extra work to test the views in isolation? > > When I use integrate_views, can I write view spec in what would > otherwise be isolated controller spec? Yes you can do this. Personally, I find it a benefit in discovery, maintenance, and readability to have simple and focused focused view and controller specs rather than a controller spec trying to do it all. There is less of a barrier when writing an isolated view spec then there is when writing a integrated controller spec. > > I read that I'm "encouraged" to do these in isolation, but IMHO the > chapter on spec'ing views is not very convincing in its own right, it > tells me that it's good, but doesn't show me as much, compared to the > examples and descriptions of circumstance that make several other > chapters very convincing. Cucumber does a great job of providing a way to drive a lot of what's in your views (especially forms). There are many times when Cucumber is sufficient. However, there are plenty of times where your views have details or a some view logic where it may not be covered with your Cucumber scenarios or it may not be clear. In these cases a very simple view spec can be a quick and effective way to drive your views. In my opinion if view specs are hard to write and difficult to maintain you're doing it wrong, either in what you're spec'ing or what you're trying to implement. Writing view specs quickly and effectively requires that you get into a groove for writing simple views that drive methods into existence on other objects. Of course, finding that groove may mean rethinking how you're writing views, and understanding and knowing what rspec and webrat gives you. And this is one of the major goals of the Rails Views chapter. To get you comfortable with how to drive a view spec and understanding what the tools provide for you. It does provides some insight into the what/when/why, but that's not its major focus at this time, more of the focus is the how. Based on my experience driving things from the views on down led to more robust code. Even though it's a simple practice, I spent less time revisiting controllers and models because the views defined what was required before I even got there. It also forced me to think outside the box and recognize the value of presenters. Presenters are one thing not covered in the book, and I'm not sure how widespread they are, but I for one find them simple and powerful tool in my toolbox. If you're interested here's a link: http://wiki.github.com/mhs/caching_presenter Thank you for sharing your frustration about this. It seems to have spurred a lot of good input, and hopefully the Rails Views chapter will be able to be updated to tackle some of the challenges you found as a reader, > > Please help. ?thanks > > Jesse > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- Zach Dennis http://www.continuousthinking.com (personal) http://www.mutuallyhuman.com (hire me) http://ideafoundry.info/behavior-driven-development (first rate BDD training) @zachdennis (twitter) From matt at mattwynne.net Wed Jul 1 13:54:09 2009 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 1 Jul 2009 18:54:09 +0100 Subject: [rspec-users] Integrate or isolate views? In-Reply-To: <85d99afe0907011014gd1bc5bcp4f1bb588479544f@mail.gmail.com> References: <03369D62-AD08-4922-8D43-8289506E9AAC@benmabey.com> <2F96CC14-B30F-48A4-9899-E75E14DB114C@ultrasaurus.com> <2c7e61990907011010r3e5b6a6fy72ec268143d671fb@mail.gmail.com> <85d99afe0907011014gd1bc5bcp4f1bb588479544f@mail.gmail.com> Message-ID: <7D8F7462-8AB1-4A99-92B9-DD4543F99FD0@mattwynne.net> On 1 Jul 2009, at 18:14, Zach Dennis wrote: > On Wed, Jul 1, 2009 at 1:10 PM, Pat Maddox > wrote: >> On Sun, Jun 28, 2009 at 11:56 AM, Sarah >> Allen wrote: >>> I find that testing views independently is useful just to catch >>> HTML errors >>> that can sometime creep in during a re-factor. These check >>> important >>> details that would be more tedious using cucumber >> >> +1 > > +1 as well. The last sentence here really nails it on the head for me, It's surprising what a wise guide an aversion to boredom can be. cheers, Matt Wynne http://mattwynne.net +447974 430184 From timcharper at gmail.com Wed Jul 1 14:57:18 2009 From: timcharper at gmail.com (Tim Harper) Date: Wed, 1 Jul 2009 12:57:18 -0600 Subject: [rspec-users] any idea why spec_server gets 'cannot remove Object' error? In-Reply-To: References: <7df13a180210246a40d87100c25a9e7d@ruby-forum.com> <57c63afe0906230719t211872abm67200c5aa1d22d5c@mail.gmail.com> Message-ID: Hi, On Thu, Jun 25, 2009 at 5:03 AM, Tom Hoen wrote: > I tried to vote a +1, but get this after clicking the Comment button: > > ? There was a problem serving the requested page. > > ? Now you're wondering, "what can I do about it!?!". Well... That's lame. votes have been coming in on other issues, so I guess that makes you and / or your timing, special :) > > Have you heard of anyone that has rspec/spork/cucumber etc running under > cygwin, colinux, andLinux, or Ulteo Virtual Desktop. Do any of them > allow for fork? yes, yes, yes, don't know cygwin is slow, I would avoid it. andLinux is a ubuntu-KDE distribution that uses colinux. I use andLinux at home, and it's simply awesome. Tim From ed.ruder at gmail.com Sat Jul 4 17:40:52 2009 From: ed.ruder at gmail.com (Ed Ruder) Date: Sat, 4 Jul 2009 14:40:52 -0700 (PDT) Subject: [rspec-users] Routes helpers work differently from helper examples compared to page rendering Message-ID: <358a910b-edea-4dce-a745-26a474f31647@f20g2000prn.googlegroups.com> I have a helper that calls a Rails route _url method. In the context of a helper spec file, the route helper method ends up calling url_for in rails/actionpack/lib/action_controller/base.rb. In the context of rendering a page, the route helper method calls url_for in rails/ actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, they can return different results, as I found out. Specifically, ActionController::Base#url_for doesn't do html escaping-- ActionView::Helpers::UrlHelper#url_for does, and has an additional parameter, :escape => true|false, to controller whether or not the escaping is done. This took some frustrating time to track down. Since the code path executed by helper examples is not the same as production, tests can produce invalid results. Also, some behavior cannot be tested, since the code generating the results is not being executed. Is this a bug in RSpec, or is there a "correct" way to test helpers that I'm missing? From dchelimsky at gmail.com Sat Jul 4 18:53:58 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 4 Jul 2009 17:53:58 -0500 Subject: [rspec-users] Routes helpers work differently from helper examples compared to page rendering In-Reply-To: <358a910b-edea-4dce-a745-26a474f31647@f20g2000prn.googlegroups.com> References: <358a910b-edea-4dce-a745-26a474f31647@f20g2000prn.googlegroups.com> Message-ID: <57c63afe0907041553s223a0c21o5023428dc29392d8@mail.gmail.com> On Sat, Jul 4, 2009 at 4:40 PM, Ed Ruder wrote: > I have a helper that calls a Rails route _url method. In the context > of a helper spec file, the route helper method ends up calling url_for > in rails/actionpack/lib/action_controller/base.rb. In the context of > rendering a page, the route helper method calls url_for in rails/ > actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, > they can return different results, as I found out. > > Specifically, ActionController::Base#url_for doesn't do html escaping-- > ActionView::Helpers::UrlHelper#url_for does, and has an additional > parameter, :escape => true|false, to controller whether or not the > escaping is done. > > This took some frustrating time to track down. > > Since the code path executed by helper examples is not the same as > production, tests can produce invalid results. Also, some behavior > cannot be tested, since the code generating the results is not being > executed. > > Is this a bug in RSpec, or is there a "correct" way to test helpers > that I'm missing? Not sure if this is directly related, but you may be interested in this ticket: https://rspec.lighthouseapp.com/projects/5645/tickets/843 From evgeny.bogdanov at gmail.com Mon Jul 6 08:05:05 2009 From: evgeny.bogdanov at gmail.com (Evgeny Bogdanov) Date: Mon, 6 Jul 2009 05:05:05 -0700 (PDT) Subject: [rspec-users] [rspec][webrat] Bug? in have_tag for Message-ID: Hello, I am writing a spec for the following html code: Home Logout and I use webrat have_tag for specying it: response.body.should have_tag('a', :href => "/home", :content => "Home") response.body.should have_tag('a', :href => "/logout", :content => "Logout") If I put :href to be "/home2" or :content to be "Home1" the spec still passes, though it shouldn't as I understand. Does somebody know what I am doing wrong? Thank you in advance, Evgeny From dchelimsky at gmail.com Mon Jul 6 08:27:38 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 6 Jul 2009 07:27:38 -0500 Subject: [rspec-users] [rspec][webrat] Bug? in have_tag for In-Reply-To: References: Message-ID: <57c63afe0907060527l32e5eb00y13e56f296fe4c59a@mail.gmail.com> On Mon, Jul 6, 2009 at 7:05 AM, Evgeny Bogdanov wrote: > Hello, > > I am writing a spec for the following html code: > Home > Logout > > and I use webrat have_tag for specying it: > response.body.should have_tag('a', :href => "/home", :content => > "Home") > response.body.should have_tag('a', :href => "/logout", :content => > "Logout") > > If I put :href to be "/home2" or :content to be "Home1" the spec still > passes, > though it shouldn't as I understand. > > Does somebody know what I am doing wrong? Try have_selector instead. I think that have_tag doesn't pay attention to the hash args (:href => etc). > Thank you in advance, > Evgeny > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From chris at silhouettesolutions.net Mon Jul 6 13:51:33 2009 From: chris at silhouettesolutions.net (Chris Sund) Date: Mon, 6 Jul 2009 10:51:33 -0700 (PDT) Subject: [rspec-users] Rspec book issue (mastermind example) Message-ID: Hey Everyone, Just a simple question. I have read and re-read the examples and I'm not getting the proper output. I'm on page 73/74 of the Rspec book and it says I should have "4 steps passed". I think the problem is within my /step_definition/mastermind.rb" file. It's definitely not beyond me to make a mistake, but I have checked and double checked my code and it all looks fine via the book examples. Here's the output I'm getting... ______________________________________________________ cucumber features Feature: code-breaker starts game As a code-breaker I want to start a game So that I can break the code Scenario: start game # features/ codebreaker_starts_game.feature:7 Given I am not yet playing # features/ step_definitions/mastermind.rb:3 When I start a new game # features/ step_definitions/mastermind.rb:7 Then the game should say "Welcome to Mastermind!" # features/ codebreaker_starts_game.feature:10 And the game should say "Enter guess:" # features/ codebreaker_starts_game.feature:11 1 scenario (1 undefined) 4 steps (2 undefined, 2 passed) 0m0.003s You can implement step definitions for undefined steps with these snippets: Then /^the game should say "([^\"]*)"$/ do |arg1| pending end _______________________________________________ It seems it doesn't think my "Then" statement is defined, but it is. My /step_definitions/mastermind.rb file looks like this... Given /^I am not yet playing$/ do end When /^I start a new game$/ do @messenger = StringIO.new game = Mastermind::Game.new(@messenger) game.start end Then /^the game should say "(.*)" $/ do |message| @messenger.string.split("\n" ).should include(message) end __________________________________________ It's either a problem with the syntax in the "Then" statement, or my "message" variable isn't getting defined right. Any help would be appreciated, and I can provide my other files, but I'm a newbie and I'm not sure what files would be worth looking at. Let me know and I'll post them. Thanks! Chris From dchelimsky at gmail.com Mon Jul 6 14:01:04 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 6 Jul 2009 13:01:04 -0500 Subject: [rspec-users] Rspec book issue (mastermind example) In-Reply-To: References: Message-ID: <57c63afe0907061101m3a674a98i5280be20be720038@mail.gmail.com> On Mon, Jul 6, 2009 at 12:51 PM, Chris Sund wrote: > Hey Everyone, > > Just a simple question. I have read and re-read the examples and I'm > not getting the proper output. ?I'm on page 73/74 of the Rspec book > and it says I should have "4 steps passed". I think the problem is > within my /step_definition/mastermind.rb" file. It's definitely not > beyond me to make a mistake, but I have checked and double checked my > code and it all looks fine via the book examples. Here's the output > I'm getting... > > ______________________________________________________ > > cucumber features > Feature: code-breaker starts game > > As a code-breaker > I want to start a game > So that I can break the code > > ?Scenario: start game ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# features/ > codebreaker_starts_game.feature:7 > ? ?Given I am not yet playing ? ? ? ? ? ? ? ? ? ? ? ?# features/ > step_definitions/mastermind.rb:3 > ? ?When I start a new game ? ? ? ? ? ? ? ? ? ? ? ? ? # features/ > step_definitions/mastermind.rb:7 > ? ?Then the game should say "Welcome to Mastermind!" # features/ > codebreaker_starts_game.feature:10 > ? ?And the game should say "Enter guess:" ? ? ? ? ? ?# features/ > codebreaker_starts_game.feature:11 > > 1 scenario (1 undefined) > 4 steps (2 undefined, 2 passed) > 0m0.003s > > You can implement step definitions for undefined steps with these > snippets: > > Then /^the game should say "([^\"]*)"$/ do |arg1| > ?pending > end > > _______________________________________________ > > It seems it doesn't think my "Then" statement is defined, but it is. > My /step_definitions/mastermind.rb file looks like this... > > Given /^I am not yet playing$/ do > end > > > When /^I start a new game$/ do > @messenger = StringIO.new > game = Mastermind::Game.new(@messenger) > game.start > end > > Then /^the game should say "(.*)" $/ do |message| There is an extra space here between "(.*)" and $. This should work: Then /^the game should say "(.*)"$/ do |message| Also, we changed the name of the game in the most recent version of the beta, so you may want to update. Cheers, David > @messenger.string.split("\n" ).should include(message) > end > > __________________________________________ > > It's either a problem with the syntax in the "Then" statement, or my > "message" variable isn't getting defined right. Any help would be > appreciated, and I can provide my other files, but I'm a newbie and > I'm not sure what files would be worth looking at. Let me know and > I'll post them. > > Thanks! > > Chris > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From chris at silhouettesolutions.net Mon Jul 6 15:31:38 2009 From: chris at silhouettesolutions.net (internetchris) Date: Mon, 6 Jul 2009 12:31:38 -0700 (PDT) Subject: [rspec-users] Rspec book issue (mastermind example) In-Reply-To: <57c63afe0907061101m3a674a98i5280be20be720038@mail.gmail.com> References: <57c63afe0907061101m3a674a98i5280be20be720038@mail.gmail.com> Message-ID: <2219a2d2-967d-431a-80a3-3b2a7e9be8ef@x25g2000prf.googlegroups.com> Beautiful, that worked great. I sent an e-mail to the pragmatic support people to get the latest version of the book. For some reason it's not showing up in my Bookshelf. Thanks David On Jul 6, 12:01?pm, David Chelimsky wrote: > On Mon, Jul 6, 2009 at 12:51 PM, Chris > > > > Sund wrote: > > Hey Everyone, > > > Just a simple question. I have read and re-read the examples and I'm > > not getting the proper output. ?I'm on page 73/74 of the Rspec book > > and it says I should have "4 steps passed". I think the problem is > > within my /step_definition/mastermind.rb" file. It's definitely not > > beyond me to make a mistake, but I have checked and double checked my > > code and it all looks fine via the book examples. Here's the output > > I'm getting... > > > ______________________________________________________ > > > cucumber features > > Feature: code-breaker starts game > > > As a code-breaker > > I want to start a game > > So that I can break the code > > > ?Scenario: start game ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# features/ > > codebreaker_starts_game.feature:7 > > ? ?Given I am not yet playing ? ? ? ? ? ? ? ? ? ? ? ?# features/ > > step_definitions/mastermind.rb:3 > > ? ?When I start a new game ? ? ? ? ? ? ? ? ? ? ? ? ? # features/ > > step_definitions/mastermind.rb:7 > > ? ?Then the game should say "Welcome to Mastermind!" # features/ > > codebreaker_starts_game.feature:10 > > ? ?And the game should say "Enter guess:" ? ? ? ? ? ?# features/ > > codebreaker_starts_game.feature:11 > > > 1 scenario (1 undefined) > > 4 steps (2 undefined, 2 passed) > > 0m0.003s > > > You can implement step definitions for undefined steps with these > > snippets: > > > Then /^the game should say "([^\"]*)"$/ do |arg1| > > ?pending > > end > > > _______________________________________________ > > > It seems it doesn't think my "Then" statement is defined, but it is. > > My /step_definitions/mastermind.rb file looks like this... > > > Given /^I am not yet playing$/ do > > end > > > When /^I start a new game$/ do > > @messenger = StringIO.new > > game = Mastermind::Game.new(@messenger) > > game.start > > end > > > Then /^the game should say "(.*)" $/ do |message| > > There is an extra space here between "(.*)" and $. This should work: > > ? Then /^the game should say "(.*)"$/ do |message| > > Also, we changed the name of the game in the most recent version of > the beta, so you may want to update. > > Cheers, > David > > > > > @messenger.string.split("\n" ).should include(message) > > end > > > __________________________________________ > > > It's either a problem with the syntax in the "Then" statement, or my > > "message" variable isn't getting defined right. Any help would be > > appreciated, and I can provide my other files, but I'm a newbie and > > I'm not sure what files would be worth looking at. Let me know and > > I'll post them. > > > Thanks! > > > Chris > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Mon Jul 6 15:43:00 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 6 Jul 2009 14:43:00 -0500 Subject: [rspec-users] Rspec book issue (mastermind example) In-Reply-To: <2219a2d2-967d-431a-80a3-3b2a7e9be8ef@x25g2000prf.googlegroups.com> References: <57c63afe0907061101m3a674a98i5280be20be720038@mail.gmail.com> <2219a2d2-967d-431a-80a3-3b2a7e9be8ef@x25g2000prf.googlegroups.com> Message-ID: <57c63afe0907061243k3f43986eu96fefb3293e24229@mail.gmail.com> On Mon, Jul 6, 2009 at 2:31 PM, internetchris wrote: > Beautiful, that worked great. I sent an e-mail to the pragmatic > support people to get the latest version of the book. For some reason > it's not showing up in my Bookshelf. Great. Thanks for reading it and going through the examples! Cheers, David > > Thanks David > > On Jul 6, 12:01?pm, David Chelimsky wrote: >> On Mon, Jul 6, 2009 at 12:51 PM, Chris >> >> >> >> Sund wrote: >> > Hey Everyone, >> >> > Just a simple question. I have read and re-read the examples and I'm >> > not getting the proper output. ?I'm on page 73/74 of the Rspec book >> > and it says I should have "4 steps passed". I think the problem is >> > within my /step_definition/mastermind.rb" file. It's definitely not >> > beyond me to make a mistake, but I have checked and double checked my >> > code and it all looks fine via the book examples. Here's the output >> > I'm getting... >> >> > ______________________________________________________ >> >> > cucumber features >> > Feature: code-breaker starts game >> >> > As a code-breaker >> > I want to start a game >> > So that I can break the code >> >> > ?Scenario: start game ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# features/ >> > codebreaker_starts_game.feature:7 >> > ? ?Given I am not yet playing ? ? ? ? ? ? ? ? ? ? ? ?# features/ >> > step_definitions/mastermind.rb:3 >> > ? ?When I start a new game ? ? ? ? ? ? ? ? ? ? ? ? ? # features/ >> > step_definitions/mastermind.rb:7 >> > ? ?Then the game should say "Welcome to Mastermind!" # features/ >> > codebreaker_starts_game.feature:10 >> > ? ?And the game should say "Enter guess:" ? ? ? ? ? ?# features/ >> > codebreaker_starts_game.feature:11 >> >> > 1 scenario (1 undefined) >> > 4 steps (2 undefined, 2 passed) >> > 0m0.003s >> >> > You can implement step definitions for undefined steps with these >> > snippets: >> >> > Then /^the game should say "([^\"]*)"$/ do |arg1| >> > ?pending >> > end >> >> > _______________________________________________ >> >> > It seems it doesn't think my "Then" statement is defined, but it is. >> > My /step_definitions/mastermind.rb file looks like this... >> >> > Given /^I am not yet playing$/ do >> > end >> >> > When /^I start a new game$/ do >> > @messenger = StringIO.new >> > game = Mastermind::Game.new(@messenger) >> > game.start >> > end >> >> > Then /^the game should say "(.*)" $/ do |message| >> >> There is an extra space here between "(.*)" and $. This should work: >> >> ? Then /^the game should say "(.*)"$/ do |message| >> >> Also, we changed the name of the game in the most recent version of >> the beta, so you may want to update. >> >> Cheers, >> David >> >> >> >> > @messenger.string.split("\n" ).should include(message) >> > end >> >> > __________________________________________ >> >> > It's either a problem with the syntax in the "Then" statement, or my >> > "message" variable isn't getting defined right. Any help would be >> > appreciated, and I can provide my other files, but I'm a newbie and >> > I'm not sure what files would be worth looking at. Let me know and >> > I'll post them. >> >> > Thanks! >> >> > Chris >> > _______________________________________________ >> > rspec-users mailing list >> > rspec-us... at rubyforge.org >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From zach.lists at gmail.com Tue Jul 7 08:17:25 2009 From: zach.lists at gmail.com (Zach Moazeni) Date: Tue, 7 Jul 2009 08:17:25 -0400 Subject: [rspec-users] should_receive with a block In-Reply-To: <50873a360907070427j3a2ae3c2qb9be11e7c06d8ffc@mail.gmail.com> References: <50873a360907070427j3a2ae3c2qb9be11e7c06d8ffc@mail.gmail.com> Message-ID: <75EEADB6-8866-4401-9870-6720DD57D6C9@gmail.com> Hey Doug, That won't work because of how Ruby handles the equality of lambdas/ procs. From my experience there are 2 approaches. 1) Test both the "call" and the block together. So instead of mocking Util, mock SfProperty. or 2) Store the lambda somewhere (like a Class variable). Test the lambda separately, and test the "call" separately, but treat the lambda as a black box. (though I'm sure you could do something funky with redefining the Class variable within the test, and asserting that the redefinition got passed in the "call" method). Testing passed lambdas/procs can be tricky because you're testing the behavior of something that _defines behavior_ and passes it to something else. On Jul 7, 2009, at 7:27 AM, doug livesey wrote: > Hi -- can I specify the block that should be called with a method? > Sort of like this: > > it "should bulk update the properties_params in groups of 50" do > block = lambda do |params| > SfProperty.put( :bulk_update, :sf_properties => params ) > end > > > Util.should_receive( :do_to_in_groups_of ).with( @properties_params, > 50, &block ) > > do_call > end > > Or am I dreaming? > Cheers, > Doug. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- Zach Moazeni http://simplechatter.com From zach.lists at gmail.com Tue Jul 7 08:29:23 2009 From: zach.lists at gmail.com (Zach Moazeni) Date: Tue, 7 Jul 2009 08:29:23 -0400 Subject: [rspec-users] [Rails] Controller testing problem In-Reply-To: <96354295919f15271ea31829c858a559@ruby-forum.com> References: <96354295919f15271ea31829c858a559@ruby-forum.com> Message-ID: <41814839-A3C3-49A2-900E-4C8CF53DC563@gmail.com> Hey Ignacy, I think I'm in the minority on this one, but I tend to keep my action logic independent of filters. Doing that, my typical controller spec begins like this: require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require File.expand_path(File.dirname(__FILE__) + '/../spec_helpers/ controller_behaviors') describe SomeController do stub_all_filters! describe ... ... end The method "stub_all_filters!" is a custom rolled, per project maintained, rspec hack that rips through Rails's internal methods to find the filters on a controller and creates a "before do ... end" that stubs each filter to "true" [1] I still use before filters because it's a good fit for many plugins such as ssl_requirement and authentication. However 1) I don't explicitly split up controller action behavior into filters and 2) the controller actions can depend on the filters indirectly by consuming methods like "self.current_user" I know you're maintaining prior-written code, but I thought this would be relevant. I hope this helps more than it confuses. [1] - http://gist.github.com/142047 On Jul 1, 2009, at 4:18 AM, Ignacy Moryc wrote: > Hi, > > I'm working on a legacy app - it was created in 1.2.5 then over time > ported to 2.3. It has absolutely no tests - and I'm trying to fix that > in between adding new functionality. > > My problem right now is that my ApplicationController is running > before_filters for setting different request variables (like current > location, etc.) and this messes up my tests - I can't test responses > properly (they are all redirects because ApplicationController derives > from SiteController which actions implement setting variables). What's > worse I can't test assigns hash - it doesn't get proper values > assigned. > > Is there a way for RSpec to skip the filters in tests? Or to stop > ApplicationController to derive from SiteController? > > Thanks, > Ignacy -- Zach Moazeni http://simplechatter.com From zach.lists at gmail.com Tue Jul 7 08:37:42 2009 From: zach.lists at gmail.com (Zach Moazeni) Date: Tue, 7 Jul 2009 08:37:42 -0400 Subject: [rspec-users] [Rails] Controller testing problem In-Reply-To: <41814839-A3C3-49A2-900E-4C8CF53DC563@gmail.com> References: <96354295919f15271ea31829c858a559@ruby-forum.com> <41814839-A3C3-49A2-900E-4C8CF53DC563@gmail.com> Message-ID: <2ED34508-60FF-449F-B64C-2ADF1EEF3DDE@gmail.com> Bleh, I forgot to mention, if you have to maintain controller action code that is dependent on instance variables set elsewhere, here is a ruby hack that will allow you to setup the context. (Please note, I recommend refactoring the code under test but I understand sometimes the preconditions of a test need to be a little....slimy. However, both the code and the spec are smelly) require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require File.expand_path(File.dirname(__FILE__) + '/../spec_helpers/ controller_behaviors') describe SomeController do stub_all_filters! describe "#foo" do def do_get get :foo end it "should do something" do controller.send(:instance_variable_set, "@bar", "here's bar") do_get end end end class SomeController < ApplicationController def foo p "need to do something with #{@bar}" end end Here's a gist in case the code formatting goes awry http://gist.github.com/142050 . On Jul 7, 2009, at 8:29 AM, Zach Moazeni wrote: > Hey Ignacy, > > I think I'm in the minority on this one, but I tend to keep my > action logic independent of filters. Doing that, my typical > controller spec begins like this: > > require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') > require File.expand_path(File.dirname(__FILE__) + '/../spec_helpers/ > controller_behaviors') > > describe SomeController do > stub_all_filters! > > describe ... > > ... > end > > The method "stub_all_filters!" is a custom rolled, per project > maintained, rspec hack that rips through Rails's internal methods to > find the filters on a controller and creates a "before do ... end" > that stubs each filter to "true" [1] > > I still use before filters because it's a good fit for many plugins > such as ssl_requirement and authentication. However 1) I don't > explicitly split up controller action behavior into filters and 2) > the controller actions can depend on the filters indirectly by > consuming methods like "self.current_user" > > I know you're maintaining prior-written code, but I thought this > would be relevant. I hope this helps more than it confuses. > > [1] - http://gist.github.com/142047 -- Zach Moazeni http://simplechatter.com From chris at silhouettesolutions.net Tue Jul 7 11:35:01 2009 From: chris at silhouettesolutions.net (Chris Sund) Date: Tue, 7 Jul 2009 08:35:01 -0700 (PDT) Subject: [rspec-users] Noob syntax questions regarding rspec book... Message-ID: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> Hey Everyone, I've been working my way through the Rspec book trying to absorb and understand everything. This is my first time with BDD and I'm just trying to figure out some simple syntax stuff. My questions revolve around some of the syntaxing used in the book. These are really simple questions. 1.) Given /^the secret code is (. . . .)$/ do |code| Is (. . . .) simply a place holder? could I use something like (- - - -) instead, or does it actually mean something? 2.) Then /^the mark should be (.*)$/ do |mark| Similar question....what does .* represent? 3.) In the following example why don't I pass |guess| to the When statement? I'm sure it has something to do with the (code.split) syntax, I'm just not sure what. When /^I guess (. . . .)$/ do |code| @game.guess(code.split) end 4.) And finally what does ("\n") do? Then /^the mark should be (.*)$/ do |mark| @messenger.string.split("\n").should include(mark) end Thank You! Chris From hayafirst at gmail.com Tue Jul 7 11:46:38 2009 From: hayafirst at gmail.com (Yi Wen) Date: Tue, 7 Jul 2009 10:46:38 -0500 Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> Message-ID: They are regex. On Jul 7, 2009, at 10:35 AM, Chris Sund wrote: > Hey Everyone, > > I've been working my way through the Rspec book trying to absorb and > understand everything. This is my first time with BDD and I'm just > trying to figure out some simple syntax stuff. My questions revolve > around some of the syntaxing used in the book. These are really simple > questions. > > > 1.) Given /^the secret code is (. . . .)$/ do |code| > Is (. . . .) simply a place holder? could I use something like > (- - - -) instead, or does it actually mean something? > > 2.) Then /^the mark should be (.*)$/ do |mark| > Similar question....what does .* represent? > > > > 3.) In the following example why don't I pass |guess| to the When > statement? I'm sure it has something to do with the (code.split) > syntax, I'm just not sure what. > > When /^I guess (. . . .)$/ do |code| > @game.guess(code.split) > end > > > 4.) And finally what does ("\n") do? > > Then /^the mark should be (.*)$/ do |mark| > @messenger.string.split("\n").should include(mark) > end > > > > > > Thank You! > > Chris > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Tue Jul 7 12:07:07 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 7 Jul 2009 11:07:07 -0500 Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> Message-ID: <57c63afe0907070907r24c83363t2f31b4f274a06436@mail.gmail.com> On Tue, Jul 7, 2009 at 10:35 AM, Chris Sund wrote: > Hey Everyone, > > I've been working my way through the Rspec book trying to absorb and > understand everything. This is my first time with BDD and I'm just > trying to figure out some simple syntax stuff. My questions revolve > around some of the syntaxing used in the book. These are really simple > questions. Hey Chris, The book assumes a basic working knowledge of Ruby. The questions you are asking are about Ruby, not about RSpec or Cucumber. I'll answer them for you here, but I don't think this is appropriate or necessary for the book itself. > 1.) Given /^the secret code is (. . . .)$/ do |code| > ? ? ? ?Is (. . . .) simply a place holder? could I use something like > (- - - -) instead, or does it actually mean something? The argument to Given is a regular expression. The "." means "any character", so (. . . .) means any four characters with spaces in between. There are certainly more specific and fool-proof ways we could express this, but this is a very simple way, and works just fine in the context of a cucumber step definition. > 2.) Then /^the mark should be (.*)$/ do |mark| > ? ? ? ?Similar question....what does .* represent? The "." means any character and the "*" means any number of times. > 3.) In the following example why don't I pass ? |guess| to the When > statement? I'm sure it has something to do with the (code.split) > syntax, I'm just not sure what. > > When /^I guess (. . . .)$/ do |code| > @game.guess(code.split) > end You're correct - code.split converts "r y g c" to ['r', 'y', 'g', 'c'], which is what the game wants to receive (an array, rather than a string). > 4.) And finally what does ("\n") do? > > Then /^the mark should be (.*)$/ do |mark| > ?@messenger.string.split("\n").should include(mark) > end The @messenger is a StringIO object. It receives puts() statements and adds them to it's string attribute with a line break ("\n") at the end so if you do this: @messenger.puts "a" @messenger.puts "b" Then the result of @messenger.string is "a\nb\n" Splitting that on "\n" results in ["a","b"], which allows you to ask if that result includes "a", for example: @messenger.split("\n").include?("a") Which can be expressed as an expectation in RSpec like this: @messenger.split("\n").should include("a") HTH, David > > > > > > Thank You! > > Chris > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From rick.denatale at gmail.com Tue Jul 7 12:12:24 2009 From: rick.denatale at gmail.com (Rick DeNatale) Date: Tue, 7 Jul 2009 12:12:24 -0400 Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> Message-ID: On Tue, Jul 7, 2009 at 11:35 AM, Chris Sund wrote: > Hey Everyone, > > I've been working my way through the Rspec book trying to absorb and > understand everything. This is my first time with BDD and I'm just > trying to figure out some simple syntax stuff. My questions revolve > around some of the syntaxing used in the book. These are really simple > questions. > > > 1.) Given /^the secret code is (. . . .)$/ do |code| > ? ? ? ?Is (. . . .) simply a place holder? could I use something like > (- - - -) instead, or does it actually mean something? Yes it actually means something. the stuff between // is a regular expression, and some of the characters have meaning. ^ means the beginning of the string $ means the end of the string Each . will mark a single character, any character will do. The parens mark a group, the part of the string which marks the group will be assigned to the code parameter. So when the whole regex matches code will be set to the four characters of the code separated by spaces. > > 2.) Then /^the mark should be (.*)$/ do |mark| > ? ? ? ?Similar question....what does .* represent? it means zero or more arbitrary characters > 3.) In the following example why don't I pass ? |guess| to the When > statement? I'm sure it has something to do with the (code.split) > syntax, I'm just not sure what. > > When /^I guess (. . . .)$/ do |code| > @game.guess(code.split) > end There isn't a variable named guess here. As I said in answer to the first question, if the story says When I guess 1 3 4 2 then when the step is executed the code parameter to the block will be set to "1 3 4 2" and "1 3 4 2".split gives ["1", "3", "4", "2"] > 4.) And finally what does ("\n") do? > > Then /^the mark should be (.*)$/ do |mark| > ?@messenger.string.split("\n").should include(mark) > end "\n" is a ruby string literal representing a new-line, so @messenger.string.split("\n") results in an array comprising each line within @messenger.string -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale From ben at benmabey.com Tue Jul 7 13:11:51 2009 From: ben at benmabey.com (Ben Mabey) Date: Tue, 07 Jul 2009 11:11:51 -0600 Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> Message-ID: <4A5381D7.6000609@benmabey.com> Rick DeNatale wrote: > On Tue, Jul 7, 2009 at 11:35 AM, Chris > Sund wrote: > >> Hey Everyone, >> >> I've been working my way through the Rspec book trying to absorb and >> understand everything. This is my first time with BDD and I'm just >> trying to figure out some simple syntax stuff. My questions revolve >> around some of the syntaxing used in the book. These are really simple >> questions. >> >> >> 1.) Given /^the secret code is (. . . .)$/ do |code| >> Is (. . . .) simply a place holder? could I use something like >> (- - - -) instead, or does it actually mean something? >> > > Yes it actually means something. the stuff between // is a regular > expression, and some of the characters have meaning. > > ^ means the beginning of the string > $ means the end of the string > Each . will mark a single character, any character will do. > The parens mark a group, the part of the string which marks the > group will be assigned to the code parameter. > So when the whole regex matches code will be set to the four > characters of the code separated by spaces. > >> 2.) Then /^the mark should be (.*)$/ do |mark| >> Similar question....what does .* represent? >> > > it means zero or more arbitrary characters > > >> 3.) In the following example why don't I pass |guess| to the When >> statement? I'm sure it has something to do with the (code.split) >> syntax, I'm just not sure what. >> >> When /^I guess (. . . .)$/ do |code| >> @game.guess(code.split) >> end >> > > There isn't a variable named guess here. As I said in answer to the > first question, if the story says > > When I guess 1 3 4 2 > > then when the step is executed the code parameter to the block will be > set to "1 3 4 2" and "1 3 4 2".split gives ["1", "3", "4", "2"] > >> 4.) And finally what does ("\n") do? >> >> Then /^the mark should be (.*)$/ do |mark| >> @messenger.string.split("\n").should include(mark) >> end >> > > "\n" is a ruby string literal representing a new-line, so > @messenger.string.split("\n") results in an array comprising each > line within @messenger.string > > > > Chris, FYI, a good resource to learn and play around with reg exps in ruby is: http://rubular.com/ -Ben From stephen.smithstone at gmail.com Tue Jul 7 11:49:02 2009 From: stephen.smithstone at gmail.com (ssmithstone) Date: Tue, 7 Jul 2009 08:49:02 -0700 (PDT) Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> Message-ID: I to am in the same situation and the meaning of the /^ $/ is a regular expression so it matches yours features so in your features is you do Give some random text I guess some more random test the code would run against that given method, hope that makes sense and that's my view of the code On Jul 7, 4:35?pm, Chris Sund wrote: > Hey Everyone, > > I've been working my way through the Rspec book trying to absorb and > understand everything. This is my first time with BDD and I'm just > trying to figure out some simple syntax stuff. My questions revolve > around some of the syntaxing used in the book. These are really simple > questions. > > 1.) Given /^the secret code is (. . . .)$/ do |code| > ? ? ? ? Is (. . . .) simply a place holder? could I use something like > (- - - -) instead, or does it actually mean something? > > 2.) Then /^the mark should be (.*)$/ do |mark| > ? ? ? ? Similar question....what does .* represent? > > 3.) In the following example why don't I pass ? |guess| to the When > statement? I'm sure it has something to do with the (code.split) > syntax, I'm just not sure what. > > When /^I guess (. . . .)$/ do |code| > @game.guess(code.split) > end > > 4.) And finally what does ("\n") do? > > Then /^the mark should be (.*)$/ do |mark| > ? @messenger.string.split("\n").should include(mark) > end > > Thank You! > > Chris > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From lists at ruby-forum.com Wed Jul 8 03:52:38 2009 From: lists at ruby-forum.com (Pirkka Hartikainen) Date: Wed, 8 Jul 2009 09:52:38 +0200 Subject: [rspec-users] Cucumber: Running a single feature / scenario In-Reply-To: <8d961d900809011330y1bcc1178qac683e6e08b22c6a@mail.gmail.com> References: <9AA10895-5708-4DBD-A807-AA7F81ED2BE8@mattwynne.net> <57c63afe0809011245o3ef36a69hd8a7aa7ad0fc01ee@mail.gmail.com> <8d961d900809011330y1bcc1178qac683e6e08b22c6a@mail.gmail.com> Message-ID: --line option has been deprecated, use the colon syntax instead. cucumber path/to/file.feature --line 33 has become cucumber path/to/file.feature:33 PS. updating this thread because of a high google page rank, it's the first search result for "cucumber running a single scenario" -- Posted via http://www.ruby-forum.com/. From ben at benmabey.com Wed Jul 8 11:33:57 2009 From: ben at benmabey.com (Ben Mabey) Date: Wed, 08 Jul 2009 09:33:57 -0600 Subject: [rspec-users] loading custom matchers into cucumber In-Reply-To: <8277b7f40907080828y79e753f6y5a1bac1eb198092e@mail.gmail.com> References: <8277b7f40907080824s1e16b41el39cd1bdb45cda5f5@mail.gmail.com> <8277b7f40907080828y79e753f6y5a1bac1eb198092e@mail.gmail.com> Message-ID: <4A54BC65.3080203@benmabey.com> Joaquin Rivera Padron wrote: > > hi there, > In a step I need to assert (don't know hoe to translate this into > "should" terms) state before keep going, example: > Perhaps you need to make an "expectation" instead of an "assert"? :) > > Given "I have signed up as tom who is an admin" do |user, role| do > # call some steps to create user tom > > end > > > oops, sorry that was gone unfinished! > > Given "I have signed up as tom who is an admin" do |user, role| do > # call some steps to create user tom > # I need to assert that tom really have the rol (this is not the > most important, so please bear with me) > user_should_have_role user, role > end > > that as you see is not very pretty, I would like to do: > > Spec::Matchers.define :have_rol do |role| > match do |user| > user.roles.collect{ |r| r.name }.include?(role) > end > end > > and then "assert": > > tom.should have_role role > > only I don't get to load the matcher. how you normally load custom > machters to be used in cucumber? Try putting your Spec::Matchers.define block in your env.rb file to see if that works. I haven't looked closely at how the new matcher DSL adds the matchers but I would think that would work. -Ben From ed.ruder at gmail.com Wed Jul 8 15:03:57 2009 From: ed.ruder at gmail.com (Ed Ruder) Date: Wed, 8 Jul 2009 12:03:57 -0700 (PDT) Subject: [rspec-users] Routes helpers work differently from helper examples compared to page rendering In-Reply-To: <57c63afe0907041553s223a0c21o5023428dc29392d8@mail.gmail.com> References: <358a910b-edea-4dce-a745-26a474f31647@f20g2000prn.googlegroups.com> <57c63afe0907041553s223a0c21o5023428dc29392d8@mail.gmail.com> Message-ID: <1c3c86ea-4145-4314-877d-b6a71779a4b5@d9g2000prh.googlegroups.com> I don't think that ticket applies to my problem--as I read it, https://rspec.lighthouseapp.com/projects/5645/tickets/843 is about enhancing RSpec with additional routing tests/specs. My issue is that in the context of a helper spec, when calling route helpers (_url/_path methods) from a helper, a different Rails code path is executed, giving different results in some cases. This means that sometimes your helper specs pass when the helper "in the wild" doesn't do what's spec'd--you cannot trust your specs! Thoughts? Ed On Jul 4, 3:53?pm, David Chelimsky wrote: > On Sat, Jul 4, 2009 at 4:40 PM, Ed Ruder wrote: > > I have a helper that calls a Rails route _url method. In the context > > of a helper spec file, the route helper method ends up calling url_for > > in rails/actionpack/lib/action_controller/base.rb. In the context of > > rendering a page, the route helper method calls url_for in rails/ > > actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, > > they can return different results, as I found out. > > > Specifically, ActionController::Base#url_for doesn't do html escaping-- > > ActionView::Helpers::UrlHelper#url_for does, and has an additional > > parameter, :escape => true|false, to controller whether or not the > > escaping is done. > > > This took some frustrating time to track down. > > > Since the code path executed by helper examples is not the same as > > production, tests can produce invalid results. Also, some behavior > > cannot be tested, since the code generating the results is not being > > executed. > > > Is this a bug in RSpec, or is there a "correct" way to test helpers > > that I'm missing? > > Not sure if this is directly related, but you may be interested in > this ticket:https://rspec.lighthouseapp.com/projects/5645/tickets/843 > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From chris at silhouettesolutions.net Thu Jul 9 00:43:35 2009 From: chris at silhouettesolutions.net (internetchris) Date: Wed, 8 Jul 2009 21:43:35 -0700 (PDT) Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> Message-ID: <3b2e2083-20f4-49e4-86ab-8972f89c2032@i8g2000pro.googlegroups.com> Hey everyone, I appreciate the replies. After I posted I realized I was looking at ruby code. It seems this is where my learning curve is going to take a leap. I understand Rails, I am starting to understand Rspec and Cucumber, and I understand programming. This is my second language, but it's the first time I have had to learn something new. I come from a foxpro background and it seems I need to dig into the Ruby language a little more. I keep forgetting that Rails is simply a framework and not the language - the same is true for Rspec. I hope to laugh at my mistakes in a year or two :-) Thanks! Chris Sund On Jul 7, 9:49?am, ssmithstone wrote: > I to am in the same situation and the meaning of the /^ $/ is a > regular expression so it matches yours features so in your features is > you do > > Give some random text I guess some more random test > > the code would run against that given method, hope that makes sense > and that's my view of the code > > On Jul 7, 4:35?pm, Chris Sund wrote: > > > Hey Everyone, > > > I've been working my way through the Rspec book trying to absorb and > > understand everything. This is my first time with BDD and I'm just > > trying to figure out some simple syntax stuff. My questions revolve > > around some of the syntaxing used in the book. These are really simple > > questions. > > > 1.) Given /^the secret code is (. . . .)$/ do |code| > > ? ? ? ? Is (. . . .) simply a place holder? could I use something like > > (- - - -) instead, or does it actually mean something? > > > 2.) Then /^the mark should be (.*)$/ do |mark| > > ? ? ? ? Similar question....what does .* represent? > > > 3.) In the following example why don't I pass ? |guess| to the When > > statement? I'm sure it has something to do with the (code.split) > > syntax, I'm just not sure what. > > > When /^I guess (. . . .)$/ do |code| > > @game.guess(code.split) > > end > > > 4.) And finally what does ("\n") do? > > > Then /^the mark should be (.*)$/ do |mark| > > ? @messenger.string.split("\n").should include(mark) > > end > > > Thank You! > > > Chris > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From chris at silhouettesolutions.net Thu Jul 9 00:51:16 2009 From: chris at silhouettesolutions.net (internetchris) Date: Wed, 8 Jul 2009 21:51:16 -0700 (PDT) Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: <3b2e2083-20f4-49e4-86ab-8972f89c2032@i8g2000pro.googlegroups.com> References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> <3b2e2083-20f4-49e4-86ab-8972f89c2032@i8g2000pro.googlegroups.com> Message-ID: In addition - Ben thanks for the link, I just checked it out. Rubular seems like an awesome little tool and I will bookmark the page. I really appreciate all of the help and hope I can return the favor one day. Thanks! Chris On Jul 8, 10:43?pm, internetchris wrote: > Hey everyone, > > I appreciate the replies. After I posted I realized I was looking at > ruby code. It seems this is where my learning curve is going to take a > leap. I understand Rails, I am starting to understand Rspec and > Cucumber, and I understand programming. This is my second language, > but it's the first time I have had to learn something new. I come from > a foxpro background and it seems I need to dig into the Ruby language > a little more. I keep forgetting that Rails is simply a framework and > not the language - the same is true for Rspec. I hope to laugh at my > mistakes in a year or two :-) > > Thanks! > > Chris Sund > > On Jul 7, 9:49?am, ssmithstone wrote: > > > I to am in the same situation and the meaning of the /^ $/ is a > > regular expression so it matches yours features so in your features is > > you do > > > Give some random text I guess some more random test > > > the code would run against that given method, hope that makes sense > > and that's my view of the code > > > On Jul 7, 4:35?pm, Chris Sund wrote: > > > > Hey Everyone, > > > > I've been working my way through the Rspec book trying to absorb and > > > understand everything. This is my first time with BDD and I'm just > > > trying to figure out some simple syntax stuff. My questions revolve > > > around some of the syntaxing used in the book. These are really simple > > > questions. > > > > 1.) Given /^the secret code is (. . . .)$/ do |code| > > > ? ? ? ? Is (. . . .) simply a place holder? could I use something like > > > (- - - -) instead, or does it actually mean something? > > > > 2.) Then /^the mark should be (.*)$/ do |mark| > > > ? ? ? ? Similar question....what does .* represent? > > > > 3.) In the following example why don't I pass ? |guess| to the When > > > statement? I'm sure it has something to do with the (code.split) > > > syntax, I'm just not sure what. > > > > When /^I guess (. . . .)$/ do |code| > > > @game.guess(code.split) > > > end > > > > 4.) And finally what does ("\n") do? > > > > Then /^the mark should be (.*)$/ do |mark| > > > ? @messenger.string.split("\n").should include(mark) > > > end > > > > Thank You! > > > > Chris > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From rick.denatale at gmail.com Thu Jul 9 09:05:09 2009 From: rick.denatale at gmail.com (Rick DeNatale) Date: Thu, 9 Jul 2009 09:05:09 -0400 Subject: [rspec-users] Noob syntax questions regarding rspec book... In-Reply-To: <3b2e2083-20f4-49e4-86ab-8972f89c2032@i8g2000pro.googlegroups.com> References: <86e86e5c-ef82-4c85-9807-6fe729ae252f@x25g2000prf.googlegroups.com> <3b2e2083-20f4-49e4-86ab-8972f89c2032@i8g2000pro.googlegroups.com> Message-ID: On Thu, Jul 9, 2009 at 12:43 AM, internetchris wrote: > Hey everyone, > > I appreciate the replies. After I posted I realized I was looking at > ruby code. It seems this is where my learning curve is going to take a > leap. I understand Rails, I am starting to understand Rspec and > Cucumber, and I understand programming. This is my second language, > but it's the first time I have had to learn something new. I come from > a foxpro background and it seems I need to dig into the Ruby language > a little more. I keep forgetting that Rails is simply a framework and > not the language - the same is true for Rspec. I hope to laugh at my > mistakes in a year or two :-) And I tend to think of regular expressions as a language within a language. Lot's of programming languages incorporate regular expressions with slight variations. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale From dchelimsky at gmail.com Thu Jul 9 10:11:55 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 9 Jul 2009 09:11:55 -0500 Subject: [rspec-users] before(:all) and nested contexts In-Reply-To: <32d7413e0907090340m3d424b3ar7068359f692b9ef1@mail.gmail.com> References: <32d7413e0907090340m3d424b3ar7068359f692b9ef1@mail.gmail.com> Message-ID: <57c63afe0907090711n1ce766edn9a9bca4388c64438@mail.gmail.com> On Thu, Jul 9, 2009 at 5:40 AM, Daniel Tenner wrote: > Hi all, > Like everyone (?), I use nested contexts to keep my specs well organised and > tidy. > However, I have a problem. I have various sets of specs that needs to > perform very time-expensive operations to set up the fixtures that will be > examined in the tests. Two specific examples: testing access control logic > (requires creating a whole tree of items to verify the correct access level > against each item), and project archival (which creates a project, fills it > with test data, archives/zips the project contents, then unzips them for > examination). > I tried using before(:all) to set up those costly fixtures, however I hit > upon a feature of rspec that made that less than successful: > When using before(:all) along with nested contexts, rspec actually re-runs > the before(:all) before each sub-context. So if, like me, you have your > specs neatly organised in sub-contexts, the before(:all) is actually re-run > many times. > Interestingly, when a before(:all) is run in the root context, rspec does > not actually remove the data from the database when re-running the > before(:all). "Great," I then thought, "I can just detect whether the data > is created and decide whether or not to create the objects on that basis". > Not so fast, though: Rspec may not clobber the database, but it does clobber > instance variables. In the case of the access control test, there's about 40 > different instance variables, so keeping track of them all manually in some > global variable outside of rspec would be messy to say the least... > So my question is, is there any workaround for successfully using > before(:all) and nested specs, so that code like the following works and > doesn't run the expensive operation more than once: > describe "Some functionality" do > ??before(:all) do > ?? ?@variable = some_expensive_operation > ??end > > ??it "should do something" do > ?? ?@variable.should do_something > ??end > > ??describe "in a specific context" do > ?? ?it "should do another thing" do > ?? ? ?@variable.should do_another_thing > ?? ?end > ??end > end > Worth noting that I'm quite happy to give up the ability to have before > blocks in the sub-contexts in order to ensure that the expensive operation > is only run once... > Your thoughts most welcome... (including, perhaps, telling me that I'm Doing > It Wrong) I wouldn't say you're doing it wrong or right. I think we really want both options, in the end - a before(:something) concept that is not inherited and one that is (before(:all)). There are a couple of tickets about this in lighthouse: https://rspec.lighthouseapp.com/projects/5645/tickets/632 https://rspec.lighthouseapp.com/projects/5645/tickets/819 There may be others. Please feel free to continue this in the convo on https://rspec.lighthouseapp.com/projects/5645/tickets/632. > Daniel Tenner > http://www.woobius.com > http://danieltenner.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From zach.lists at gmail.com Thu Jul 9 10:18:41 2009 From: zach.lists at gmail.com (Zach Moazeni) Date: Thu, 9 Jul 2009 10:18:41 -0400 Subject: [rspec-users] before(:all) and nested contexts In-Reply-To: <32d7413e0907090340m3d424b3ar7068359f692b9ef1@mail.gmail.com> References: <32d7413e0907090340m3d424b3ar7068359f692b9ef1@mail.gmail.com> Message-ID: <55B9FE1C-CA1B-4758-AB72-88C0E55FA762@gmail.com> Hey Dan, 1 approach you could do is define a method within the outer describe that is called within the inner describe and within each test not contained by an inner describe. describe "Some functionality" do it "should do something" do @variable = some_expensive_operation @variable.should do_something end describe "in a specific context" do before(:all) do @variable = some_expensive_operation end it "should do another thing" do @variable.should do_another_thing end end def some_expensive_operation p "in here" end end (also uploaded to http://gist.github.com/143693) My solution is to unDRY the subcontext's preconditions. I have pretty strong opinions about DRYing up specs at the cost of grokability. On Jul 9, 2009, at 6:40 AM, Daniel Tenner wrote: > Hi all, > > Like everyone (?), I use nested contexts to keep my specs well > organised and tidy. > > However, I have a problem. I have various sets of specs that needs > to perform very time-expensive operations to set up the fixtures > that will be examined in the tests. Two specific examples: testing > access control logic (requires creating a whole tree of items to > verify the correct access level against each item), and project > archival (which creates a project, fills it with test data, archives/ > zips the project contents, then unzips them for examination). > > I tried using before(:all) to set up those costly fixtures, however > I hit upon a feature of rspec that made that less than successful: > > When using before(:all) along with nested contexts, rspec actually > re-runs the before(:all) before each sub-context. So if, like me, > you have your specs neatly organised in sub-contexts, the > before(:all) is actually re-run many times. > > Interestingly, when a before(:all) is run in the root context, rspec > does not actually remove the data from the database when re-running > the before(:all). "Great," I then thought, "I can just detect > whether the data is created and decide whether or not to create the > objects on that basis". Not so fast, though: Rspec may not clobber > the database, but it does clobber instance variables. In the case of > the access control test, there's about 40 different instance > variables, so keeping track of them all manually in some global > variable outside of rspec would be messy to say the least... > > So my question is, is there any workaround for successfully using > before(:all) and nested specs, so that code like the following works > and doesn't run the expensive operation more than once: > > describe "Some functionality" do > before(:all) do > @variable = some_expensive_operation > end > > it "should do something" do > @variable.should do_something > end > > describe "in a specific context" do > it "should do another thing" do > @variable.should do_another_thing > end > end > end > > Worth noting that I'm quite happy to give up the ability to have > before blocks in the sub-contexts in order to ensure that the > expensive operation is only run once... > > Your thoughts most welcome... (including, perhaps, telling me that > I'm Doing It Wrong) > > Daniel Tenner > http://www.woobius.com > http://danieltenner.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- Zach Moazeni http://simplechatter.com From zach.lists at gmail.com Thu Jul 9 10:22:53 2009 From: zach.lists at gmail.com (Zach Moazeni) Date: Thu, 9 Jul 2009 10:22:53 -0400 Subject: [rspec-users] before(:all) and nested contexts In-Reply-To: <55B9FE1C-CA1B-4758-AB72-88C0E55FA762@gmail.com> References: <32d7413e0907090340m3d424b3ar7068359f692b9ef1@mail.gmail.com> <55B9FE1C-CA1B-4758-AB72-88C0E55FA762@gmail.com> Message-ID: <89A30079-988A-4F2B-8CA4-6EFB8D60BAC9@gmail.com> Rereading your original email, I'm thinking I may not have entirely understood your situation. 1) Looks like you've already defined the "some_expensive_operation" (but that's minor) 2) Is it your goal to call "some_expensive_operation" once and only once? On Jul 9, 2009, at 10:18 AM, Zach Moazeni wrote: > Hey Dan, > > 1 approach you could do is define a method within the outer describe > that is called within the inner describe and within each test not > contained by an inner describe. > > describe "Some functionality" do > it "should do something" do > @variable = some_expensive_operation > @variable.should do_something > end > > describe "in a specific context" do > before(:all) do > @variable = some_expensive_operation > end > > it "should do another thing" do > @variable.should do_another_thing > end > end > > def some_expensive_operation > p "in here" > end > end > > (also uploaded to http://gist.github.com/143693) > > My solution is to unDRY the subcontext's preconditions. I have > pretty strong opinions about DRYing up specs at the cost of > grokability. > > On Jul 9, 2009, at 6:40 AM, Daniel Tenner wrote: > >> Hi all, >> >> Like everyone (?), I use nested contexts to keep my specs well >> organised and tidy. >> >> However, I have a problem. I have various sets of specs that needs >> to perform very time-expensive operations to set up the fixtures >> that will be examined in the tests. Two specific examples: testing >> access control logic (requires creating a whole tree of items to >> verify the correct access level against each item), and project >> archival (which creates a project, fills it with test data, >> archives/zips the project contents, then unzips them for >> examination). >> >> I tried using before(:all) to set up those costly fixtures, however >> I hit upon a feature of rspec that made that less than successful: >> >> When using before(:all) along with nested contexts, rspec actually >> re-runs the before(:all) before each sub-context. So if, like me, >> you have your specs neatly organised in sub-contexts, the >> before(:all) is actually re-run many times. >> >> Interestingly, when a before(:all) is run in the root context, >> rspec does not actually remove the data from the database when re- >> running the before(:all). "Great," I then thought, "I can just >> detect whether the data is created and decide whether or not to >> create the objects on that basis". Not so fast, though: Rspec may >> not clobber the database, but it does clobber instance variables. >> In the case of the access control test, there's about 40 different >> instance variables, so keeping track of them all manually in some >> global variable outside of rspec would be messy to say the least... >> >> So my question is, is there any workaround for successfully using >> before(:all) and nested specs, so that code like the following >> works and doesn't run the expensive operation more than once: >> >> describe "Some functionality" do >> before(:all) do >> @variable = some_expensive_operation >> end >> >> it "should do something" do >> @variable.should do_something >> end >> >> describe "in a specific context" do >> it "should do another thing" do >> @variable.should do_another_thing >> end >> end >> end >> >> Worth noting that I'm quite happy to give up the ability to have >> before blocks in the sub-contexts in order to ensure that the >> expensive operation is only run once... >> >> Your thoughts most welcome... (including, perhaps, telling me that >> I'm Doing It Wrong) >> >> Daniel Tenner >> http://www.woobius.com >> http://danieltenner.com >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > -- > Zach Moazeni > http://simplechatter.com > > > -- Zach Moazeni http://simplechatter.com From dchelimsky at gmail.com Thu Jul 9 10:40:13 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 9 Jul 2009 09:40:13 -0500 Subject: [rspec-users] before(:all) and nested contexts In-Reply-To: <32d7413e0907090737u654edb77te52114339c7cc3c0@mail.gmail.com> References: <32d7413e0907090340m3d424b3ar7068359f692b9ef1@mail.gmail.com> <55B9FE1C-CA1B-4758-AB72-88C0E55FA762@gmail.com> <89A30079-988A-4F2B-8CA4-6EFB8D60BAC9@gmail.com> <32d7413e0907090737u654edb77te52114339c7cc3c0@mail.gmail.com> Message-ID: <57c63afe0907090740h51df54dexe36f3cbc6d2412d9@mail.gmail.com> On Thu, Jul 9, 2009 at 9:37 AM, Daniel Tenner wrote: >> 2) Is it your goal to call "some_expensive_operation" once and only once? > > Yes, exactly. In the case of the project archival spec, creating a project, > archiving it, and then unzipping it to a temporary location to look at what > was archived is a process that takes about 3 seconds on my machine. Since > archival isn't something people will be doing live on the site, speed is not > an issue from a user point of view. However, there's about 25 specs (in > about a dozen sub-contexts) examining the zip output... multiply that by 3 > seconds each, and that takes more time to run than all the other specs > combined! > I have actually managed to get this done using the method described in my > blog post @ > http://www.swombat.com/getting-rspec-beforeall-and-nested-contexts-w , but I > have to admit it's a bit hackish. > David -?":apply_to_nested?=>?false" seems like a great way to do it, but I > understand why you'd want to get the refactoring out of the way first. With > my temporary hack in place, I can wait. My message about refactoring was back in January :) If you're interested in supplying a patch I'd be interested in accepting it. > Thanks, > Daniel > On Thu, Jul 9, 2009 at 3:22 PM, Zach Moazeni wrote: >> >> Rereading your original email, I'm thinking I may not have entirely >> understood your situation. >> >> 1) Looks like you've already defined the "some_expensive_operation" (but >> that's minor) >> >> 2) Is it your goal to call "some_expensive_operation" once and only once? >> >> On Jul 9, 2009, at 10:18 AM, Zach Moazeni wrote: >> >>> Hey Dan, >>> >>> 1 approach you could do is define a method within the outer describe that >>> is called within the inner describe and within each test not contained by an >>> inner describe. >>> >>> describe "Some functionality" do >>> ?it "should do something" do >>> ? @variable = some_expensive_operation >>> ? @variable.should do_something >>> ?end >>> >>> ?describe "in a specific context" do >>> ? before(:all) do >>> ? ? @variable = some_expensive_operation >>> ? end >>> >>> ? it "should do another thing" do >>> ? ? @variable.should do_another_thing >>> ? end >>> ?end >>> >>> ?def some_expensive_operation >>> ? p "in here" >>> ?end >>> end >>> >>> (also uploaded to http://gist.github.com/143693) >>> >>> My solution is to unDRY the subcontext's preconditions. I have pretty >>> strong opinions about DRYing up specs at the cost of grokability. >>> >>> On Jul 9, 2009, at 6:40 AM, Daniel Tenner wrote: >>> >>>> Hi all, >>>> >>>> Like everyone (?), I use nested contexts to keep my specs well organised >>>> and tidy. >>>> >>>> However, I have a problem. I have various sets of specs that needs to >>>> perform very time-expensive operations to set up the fixtures that will be >>>> examined in the tests. Two specific examples: testing access control logic >>>> (requires creating a whole tree of items to verify the correct access level >>>> against each item), and project archival (which creates a project, fills it >>>> with test data, archives/zips the project contents, then unzips them for >>>> examination). >>>> >>>> I tried using before(:all) to set up those costly fixtures, however I >>>> hit upon a feature of rspec that made that less than successful: >>>> >>>> When using before(:all) along with nested contexts, rspec actually >>>> re-runs the before(:all) before each sub-context. So if, like me, you have >>>> your specs neatly organised in sub-contexts, the before(:all) is actually >>>> re-run many times. >>>> >>>> Interestingly, when a before(:all) is run in the root context, rspec >>>> does not actually remove the data from the database when re-running the >>>> before(:all). "Great," I then thought, "I can just detect whether the data >>>> is created and decide whether or not to create the objects on that basis". >>>> Not so fast, though: Rspec may not clobber the database, but it does clobber >>>> instance variables. In the case of the access control test, there's about 40 >>>> different instance variables, so keeping track of them all manually in some >>>> global variable outside of rspec would be messy to say the least... >>>> >>>> So my question is, is there any workaround for successfully using >>>> before(:all) and nested specs, so that code like the following works and >>>> doesn't run the expensive operation more than once: >>>> >>>> describe "Some functionality" do >>>> ?before(:all) do >>>> ? @variable = some_expensive_operation >>>> ?end >>>> >>>> ?it "should do something" do >>>> ? @variable.should do_something >>>> ?end >>>> >>>> ?describe "in a specific context" do >>>> ? it "should do another thing" do >>>> ? ? @variable.should do_another_thing >>>> ? end >>>> ?end >>>> end >>>> >>>> Worth noting that I'm quite happy to give up the ability to have before >>>> blocks in the sub-contexts in order to ensure that the expensive operation >>>> is only run once... >>>> >>>> Your thoughts most welcome... (including, perhaps, telling me that I'm >>>> Doing It Wrong) >>>> >>>> Daniel Tenner >>>> http://www.woobius.com >>>> http://danieltenner.com >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> -- >>> Zach Moazeni >>> http://simplechatter.com >>> >>> >>> >> >> -- >> Zach Moazeni >> http://simplechatter.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 g.zhen.ning at gmail.com Fri Jul 10 02:06:23 2009 From: g.zhen.ning at gmail.com (nnn) Date: Thu, 9 Jul 2009 23:06:23 -0700 (PDT) Subject: [rspec-users] undefined method `autorun' for Spec::Runner:Module (NoMethodError)? Message-ID: /home/jack/vendor/plugins/rspec/lib/spec/autorun.rb:3: undefined method `autorun' for Spec::Runner:Module (NoMethodError) from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from /home/jack/vendor/rails/activesupport/lib/active_support/ dependencies.rb:158:in `require' when I switch to spec/model and run spec admin_spec.rb it throw me a error like this, so how to deal with it? From dchelimsky at gmail.com Fri Jul 10 06:08:55 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 10 Jul 2009 05:08:55 -0500 Subject: [rspec-users] undefined method `autorun' for Spec::Runner:Module (NoMethodError)? In-Reply-To: References: Message-ID: <57c63afe0907100308g2cf9df4dn571f2babed8fd71@mail.gmail.com> On Fri, Jul 10, 2009 at 1:06 AM, nnn wrote: > /home/jack/vendor/plugins/rspec/lib/spec/autorun.rb:3: undefined > method `autorun' for Spec::Runner:Module (NoMethodError) > ? ? ? ?from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in > `gem_original_require' > ? ? ? ?from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in > `require' > ? ? ? ?from /home/jack/vendor/rails/activesupport/lib/active_support/ > dependencies.rb:158:in `require' > > > > when I switch to ?spec/model and run spec admin_spec.rb > it throw me a error like this, so how to deal with it? As things stand right now, you've got to run specs from the project root. This is a known issue (there is a ticket in lighthouse but naturally I can't find it now). From jdavis at medsagetechnologies.com Fri Jul 10 13:42:46 2009 From: jdavis at medsagetechnologies.com (jendavis) Date: Fri, 10 Jul 2009 10:42:46 -0700 (PDT) Subject: [rspec-users] stricter routing specs with conditional routes In-Reply-To: <24431748.post@talk.nabble.com> References: <98f0d405-8840-4228-98b9-081f1acf5653@o36g2000yqh.googlegroups.com> <24431748.post@talk.nabble.com> Message-ID: <24431870.post@talk.nabble.com> Thank you! This solved my problem. Maksim Horbul wrote: > > Here is the solution for that issue: > > it "should map {:action => 'edit'} to /admin/users/1" do > assert_generates("/admin/users/1", :controller => 'customer/users/1', > :action => 'edit') > end > -- View this message in context: http://www.nabble.com/stricter-routing-specs-with-conditional-routes-tp22691023p24431870.html Sent from the rspec-users mailing list archive at Nabble.com. From max at gorbul.net Fri Jul 10 13:34:02 2009 From: max at gorbul.net (Maksim Horbul) Date: Fri, 10 Jul 2009 10:34:02 -0700 (PDT) Subject: [rspec-users] stricter routing specs with conditional routes In-Reply-To: <98f0d405-8840-4228-98b9-081f1acf5653@o36g2000yqh.googlegroups.com> References: <98f0d405-8840-4228-98b9-081f1acf5653@o36g2000yqh.googlegroups.com> Message-ID: <24431748.post@talk.nabble.com> Here is the solution for that issue: it "should map {:action => 'edit'} to /admin/users/1" do assert_generates("/admin/users/1", :controller => 'customer/users/1', :action => 'edit') end -- View this message in context: http://www.nabble.com/stricter-routing-specs-with-conditional-routes-tp22691023p24431748.html Sent from the rspec-users mailing list archive at Nabble.com. From magic6435 at gmail.com Sat Jul 11 11:42:51 2009 From: magic6435 at gmail.com (Michael) Date: Sat, 11 Jul 2009 08:42:51 -0700 (PDT) Subject: [rspec-users] Validations are making me want to scream. help please Message-ID: Simply testing that my validations are working for a model called Project. MODEL class Project < ActiveRecord::Base validates_presence_of :brief end SPEC require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Project do before(:each) do @project = Project.new(:title => "project title", :brief => "project brief") end it "should create a new instance given valid attributes" do @project.should be_valid end end ERROR ActiveRecord::RecordInvalid in 'Project should create a new instance given valid attributes' Validation failed: Brief can't be blank POST How on earth can that be failing? its driving me nuts, i have a few other tests that check the exact same way(method taken from the Rspec book) so why all the sudden would it stop working on a new model. Blah thanks for any help. From dchelimsky at gmail.com Sat Jul 11 11:50:27 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 11 Jul 2009 10:50:27 -0500 Subject: [rspec-users] Validations are making me want to scream. help please In-Reply-To: References: Message-ID: <57c63afe0907110850j3693b7a8p12be0b43c0faace8@mail.gmail.com> On Sat, Jul 11, 2009 at 10:42 AM, Michael wrote: > Simply testing that my validations are working for a model called > Project. > > MODEL > class Project < ActiveRecord::Base > ?validates_presence_of :brief > end > > SPEC > require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') > > describe Project do > ?before(:each) do > ? ?@project = Project.new(:title => "project title", :brief => > "project brief") > ?end > > ?it "should create a new instance given valid attributes" do > ? ?@project.should be_valid > ?end > end > > ERROR > ActiveRecord::RecordInvalid in 'Project should create a new instance > given valid attributes' > Validation failed: Brief can't be blank > > > POST > How on earth can that be failing? its driving me nuts, i have a few > other tests that check the exact same way(method taken from the Rspec > book) so why all the sudden would it stop working on a new model. Blah > thanks for any help. The fact that it's raising an error suggests that something is trying to save the Project. Otherwise you'd just get back something like "expected valid? to be true". Since rspec-rails doesn't try to save the record, my guess is that there is some other code introducing itself. Either in the application code, a plugin, or perhaps an rspec extension library that has its own be_valid matcher. Any of that feasible? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From hayafirst at gmail.com Sat Jul 11 13:00:38 2009 From: hayafirst at gmail.com (Yi Wen) Date: Sat, 11 Jul 2009 12:00:38 -0500 Subject: [rspec-users] Validations are making me want to scream. help please In-Reply-To: References: Message-ID: There is a validation error, which means you were trying to save the object somewhere. Check both your tests and code, to find out where. On Jul 11, 2009, at 10:42 AM, Michael wrote: > Simply testing that my validations are working for a model called > Project. > > MODEL > class Project < ActiveRecord::Base > validates_presence_of :brief > end > > SPEC > require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') > > describe Project do > before(:each) do > @project = Project.new(:title => "project title", :brief => > "project brief") > end > > it "should create a new instance given valid attributes" do > @project.should be_valid > end > end > > ERROR > ActiveRecord::RecordInvalid in 'Project should create a new instance > given valid attributes' > Validation failed: Brief can't be blank > > > POST > How on earth can that be failing? its driving me nuts, i have a few > other tests that check the exact same way(method taken from the Rspec > book) so why all the sudden would it stop working on a new model. Blah > thanks for any help. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From chris at silhouettesolutions.net Sat Jul 11 15:29:30 2009 From: chris at silhouettesolutions.net (Chris Sund) Date: Sat, 11 Jul 2009 12:29:30 -0700 (PDT) Subject: [rspec-users] explanation of "describe" vs "context"... Message-ID: <5b3073ed-2242-40be-8f49-0ee771e7985c@o9g2000prg.googlegroups.com> Hey everyone, This is a noob question. I'm not grasping the difference between "describe" and "context" in my spec file. As an example, what's the difference with this... describe Game do context "starting up" do it "should send a welcome message" do @messenger.should_receive(:puts).with("Welcome to Mastermind!") @game.start(%w[r g y c]) end And this.... describe Game do describe "starting up" do it "should send a welcome message" do @messenger.should_receive(:puts).with("Welcome to Mastermind!") @game.start(%w[r g y c]) end Is this just preference, or are the serious differences? From zach.dennis at gmail.com Sat Jul 11 15:36:27 2009 From: zach.dennis at gmail.com (Zach Dennis) Date: Sat, 11 Jul 2009 15:36:27 -0400 Subject: [rspec-users] explanation of "describe" vs "context"... In-Reply-To: <5b3073ed-2242-40be-8f49-0ee771e7985c@o9g2000prg.googlegroups.com> References: <5b3073ed-2242-40be-8f49-0ee771e7985c@o9g2000prg.googlegroups.com> Message-ID: <85d99afe0907111236o6bb1b2a9ob7e7131a7acbef4b@mail.gmail.com> On Sat, Jul 11, 2009 at 3:29 PM, Chris Sund wrote: > Hey everyone, > > This is a noob question. I'm not grasping the difference between > "describe" and "context" in my spec file. As an example, what's the > difference with this... > > describe Game do > ? ? context "starting up" do > it "should send a welcome message" do > ? ? ? ?@messenger.should_receive(:puts).with("Welcome to > Mastermind!") > ? ? ? ?@game.start(%w[r g y c]) > ? ? ?end > > > > And this.... > > describe Game do > ? ? describe "starting up" do > it "should send a welcome message" do > ? ? ? ?@messenger.should_receive(:puts).with("Welcome to > Mastermind!") > ? ? ? ?@game.start(%w[r g y c]) > ? ? ?end > > > Is this just preference, or are the serious differences? > There is no technical difference. context is aliased to describe. However, you can use them in combination to write more expressive specs. For example: describe Game do describe "#join_game" do context "when the game has not started"d o it "should allow a player to join" end context "when the game has started" do it "should not allow another player to join" end end end > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- Zach Dennis http://www.continuousthinking.com (personal) http://www.mutuallyhuman.com (hire me) http://ideafoundry.info/behavior-driven-development (first rate BDD training) @zachdennis (twitter) From lists at ruby-forum.com Sat Jul 11 22:01:50 2009 From: lists at ruby-forum.com (Robert Rouse) Date: Sun, 12 Jul 2009 04:01:50 +0200 Subject: [rspec-users] ZenTest, RSpec and Ruby 1.9.1 Message-ID: <0f5b0bc041bef766e1d543efd3e19173@ruby-forum.com> Hello, I thought maybe you guys could help. I'm trying to use RSpec along with ZenTest. However, all I seem to get back when trying to run tests is an exception. Is there a place I can find out what that exception is? I have autospec set up to do the notifications through growl. The growl output only tells me there was an exception. I'd like to get it working. Any ideas? -- Posted via http://www.ruby-forum.com/. From ben at benmabey.com Sun Jul 12 02:18:39 2009 From: ben at benmabey.com (Ben Mabey) Date: Sun, 12 Jul 2009 00:18:39 -0600 Subject: [rspec-users] ZenTest, RSpec and Ruby 1.9.1 In-Reply-To: <0f5b0bc041bef766e1d543efd3e19173@ruby-forum.com> References: <0f5b0bc041bef766e1d543efd3e19173@ruby-forum.com> Message-ID: <4A59803F.9040901@benmabey.com> Robert Rouse wrote: > Hello, > > I thought maybe you guys could help. I'm trying to use RSpec along with > ZenTest. > > However, all I seem to get back when trying to run tests is an > exception. > > Is there a place I can find out what that exception is? I have autospec > set up to do the notifications through growl. The growl output only > tells me there was an exception. > > I'd like to get it working. > > Any ideas? > The output should be in the terminal window that you started autotest in.... What are you seeing exactly? Please post your output. Do you have the latest ZenTest and RSpec gems? Also, are you using the "autospec" command that comes with RSpec? -Ben From scott at railsnewbie.com Sun Jul 12 15:48:06 2009 From: scott at railsnewbie.com (Scott Taylor) Date: Sun, 12 Jul 2009 15:48:06 -0400 Subject: [rspec-users] explanation of "describe" vs "context"... In-Reply-To: <5b3073ed-2242-40be-8f49-0ee771e7985c@o9g2000prg.googlegroups.com> References: <5b3073ed-2242-40be-8f49-0ee771e7985c@o9g2000prg.googlegroups.com> Message-ID: <4A5A3DF6.2090707@railsnewbie.com> Chris Sund wrote: > Hey everyone, > > This is a noob question. I'm not grasping the difference between > "describe" and "context" in my spec file. As an example, what's the > difference with this... > > describe Game do > context "starting up" do > it "should send a welcome message" do > @messenger.should_receive(:puts).with("Welcome to > Mastermind!") > @game.start(%w[r g y c]) > end > > > > And this.... > > describe Game do > describe "starting up" do > it "should send a welcome message" do > @messenger.should_receive(:puts).with("Welcome to > Mastermind!") > @game.start(%w[r g y c]) > end > > > Is this just preference, or are the serious differences? > > > They are aliased, so there is no functional difference: http://gist.github.com/ded8be0839e77c30afe1 Scott > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From chris at silhouettesolutions.net Sun Jul 12 16:51:01 2009 From: chris at silhouettesolutions.net (internetchris) Date: Sun, 12 Jul 2009 13:51:01 -0700 (PDT) Subject: [rspec-users] explanation of "describe" vs "context"... In-Reply-To: <4A5A3DF6.2090707@railsnewbie.com> References: <5b3073ed-2242-40be-8f49-0ee771e7985c@o9g2000prg.googlegroups.com> <4A5A3DF6.2090707@railsnewbie.com> Message-ID: <7787e6b4-68c0-42b0-af19-1b9806c3ea7f@m7g2000prd.googlegroups.com> Thanks, that helps me understand better. On Jul 12, 1:48?pm, Scott Taylor wrote: > Chris Sund wrote: > > Hey everyone, > > > This is a noob question. I'm not grasping the difference between > > "describe" and "context" in my spec file. As an example, what's the > > difference with this... > > > describe Game do > > ? ? ?context "starting up" do > > it "should send a welcome message" do > > ? ? ? ? @messenger.should_receive(:puts).with("Welcome to > > Mastermind!") > > ? ? ? ? @game.start(%w[r g y c]) > > ? ? ? end > > > And this.... > > > describe Game do > > ? ? ?describe "starting up" do > > it "should send a welcome message" do > > ? ? ? ? @messenger.should_receive(:puts).with("Welcome to > > Mastermind!") > > ? ? ? ? @game.start(%w[r g y c]) > > ? ? ? end > > > Is this just preference, or are the serious differences? > > They are aliased, so there is no functional difference: > > http://gist.github.com/ded8be0839e77c30afe1 > > Scott > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From scott at railsnewbie.com Mon Jul 13 12:58:20 2009 From: scott at railsnewbie.com (Scott Taylor) Date: Mon, 13 Jul 2009 12:58:20 -0400 Subject: [rspec-users] Stubbing out Time correctly In-Reply-To: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> References: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> Message-ID: <5CDF941C-3FC3-4DC4-8709-638F8FDBA539@railsnewbie.com> On Jul 13, 2009, at 12:09 PM, Adam Anderson wrote: > I can't seem to find a good way to do this. If I stub out Time.now > in one of my specs but need to return it to its original > functionality then can I remove the stub? > > So I'd like to say something like: > Time.stub!(:now).and_return(foo_time) > Time.now # => foo_time > Time.unstub!(:now) > Time.now # => whatever time it actually is Capture it before you stub it: real_now= Time.now Time.stub!(:now).and_return a_later_time Time.stub!(:now).and_return real_now > > Is this possible? I noticed $rspec_mocks.reset_all, but obviously I > don't want everything to go away, just this one stub. > > > -Adam > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Mon Jul 13 14:12:45 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 13 Jul 2009 13:12:45 -0500 Subject: [rspec-users] Stubbing out Time correctly In-Reply-To: <8AFC496E-83EA-4700-8581-E95E365B054F@railsnewbie.com> References: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> <5CDF941C-3FC3-4DC4-8709-638F8FDBA539@railsnewbie.com> <8d18f1f80907131032v7a0bcaddw573be5777cf4dd28@mail.gmail.com> <92D9F3B1-437D-45A7-93F2-029BBC56C596@railsnewbie.com> <8AFC496E-83EA-4700-8581-E95E365B054F@railsnewbie.com> Message-ID: <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> On Mon, Jul 13, 2009 at 1:04 PM, Scott Taylor wrote: > > On Jul 13, 2009, at 1:46 PM, Scott Taylor wrote: > > On Jul 13, 2009, at 1:32 PM, Adam Anderson wrote: > > Thanks for the reply, Scott. > > What you describe is what is currently being done. > > now = Time.now > Time.stub!(:now).and_return(foo_time) > do_stuff > Time.stub!(:now).and_return(now) > > However, this is not returning the actual time it is only returning the time > set on the first line. I need Time.now to return to its original behavior > (i.e., returning the current, actual time). > > Well, every time you create a stub, rspec aliased the method before it > destroys it: > http://gist.github.com/1ed96143092a02cb727b > It's ugly, but it will work. > > Here's another solution, without needing to resort to the library: > http://gist.github.com/e573cb79073805a632c9 > Of course, this solution only works if you don't have other stubs on Time. It also only works as long as that method keeps its name :) That method is explicitly :nodoc:'d which indicates that it is an internal method and therefore subject to change. Not saying I plan to change it, just that of now it is not a public API. That said, this thread suggests that we need a public API for resetting partial stubs like this. I'm thinking something more intention revealing like tear_down_rspec_stubs or something like that. Thoughts? Recommendations? > Scott > > > You might also want to check out this library: > http://github.com/notahat/time_travel/tree/master > Scott > > > -Adam > > On Mon, Jul 13, 2009 at 9:58 AM, Scott Taylor wrote: >> >> On Jul 13, 2009, at 12:09 PM, Adam Anderson wrote: >> >>> I can't seem to find a good way to do this. If I stub out Time.now in one >>> of my specs but need to return it to its original functionality then can I >>> remove the stub? >>> >>> So I'd like to say something like: >>> Time.stub!(:now).and_return(foo_time) >>> Time.now # => foo_time >>> Time.unstub!(:now) >>> Time.now # => whatever time it actually is >> >> >> Capture it before you stub it: >> >> real_now= Time.now >> >> Time.stub!(:now).and_return a_later_time >> >> Time.stub!(:now).and_return real_now >> >>> >>> Is this possible? I noticed $rspec_mocks.reset_all, but obviously I don't >>> want everything to go away, just this one stub. >>> >>> >>> -Adam >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From scott at railsnewbie.com Mon Jul 13 14:15:35 2009 From: scott at railsnewbie.com (Scott Taylor) Date: Mon, 13 Jul 2009 14:15:35 -0400 Subject: [rspec-users] Stubbing out Time correctly In-Reply-To: <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> References: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> <5CDF941C-3FC3-4DC4-8709-638F8FDBA539@railsnewbie.com> <8d18f1f80907131032v7a0bcaddw573be5777cf4dd28@mail.gmail.com> <92D9F3B1-437D-45A7-93F2-029BBC56C596@railsnewbie.com> <8AFC496E-83EA-4700-8581-E95E365B054F@railsnewbie.com> <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> Message-ID: <978F1EF0-C1BA-4979-949F-0B8CE4B2C314@railsnewbie.com> On Jul 13, 2009, at 2:12 PM, David Chelimsky wrote: > On Mon, Jul 13, 2009 at 1:04 PM, Scott Taylor > wrote: >> >> On Jul 13, 2009, at 1:46 PM, Scott Taylor wrote: >> >> On Jul 13, 2009, at 1:32 PM, Adam Anderson wrote: >> >> Thanks for the reply, Scott. >> >> What you describe is what is currently being done. >> >> now = Time.now >> Time.stub!(:now).and_return(foo_time) >> do_stuff >> Time.stub!(:now).and_return(now) >> >> However, this is not returning the actual time it is only returning >> the time >> set on the first line. I need Time.now to return to its original >> behavior >> (i.e., returning the current, actual time). >> >> Well, every time you create a stub, rspec aliased the method before >> it >> destroys it: >> http://gist.github.com/1ed96143092a02cb727b >> It's ugly, but it will work. >> >> Here's another solution, without needing to resort to the library: >> http://gist.github.com/e573cb79073805a632c9 >> Of course, this solution only works if you don't have other stubs >> on Time. > > It also only works as long as that method keeps its name :) That > method is explicitly :nodoc:'d which indicates that it is an internal > method and therefore subject to change. Not saying I plan to change > it, just that of now it is not a public API. > > That said, this thread suggests that we need a public API for > resetting partial stubs like this. I'm thinking something more > intention revealing like tear_down_rspec_stubs or something like that. > Thoughts? Recommendations? I'd like unstub! to match stub!. Might also consider renaming rspec_reset, or making it part of the public api. Scott > >> Scott >> >> >> You might also want to check out this library: >> http://github.com/notahat/time_travel/tree/master >> Scott >> >> >> -Adam >> >> On Mon, Jul 13, 2009 at 9:58 AM, Scott Taylor >> wrote: >>> >>> On Jul 13, 2009, at 12:09 PM, Adam Anderson wrote: >>> >>>> I can't seem to find a good way to do this. If I stub out >>>> Time.now in one >>>> of my specs but need to return it to its original functionality >>>> then can I >>>> remove the stub? >>>> >>>> So I'd like to say something like: >>>> Time.stub!(:now).and_return(foo_time) >>>> Time.now # => foo_time >>>> Time.unstub!(:now) >>>> Time.now # => whatever time it actually is >>> >>> >>> Capture it before you stub it: >>> >>> real_now= Time.now >>> >>> Time.stub!(:now).and_return a_later_time >>> >>> Time.stub!(:now).and_return real_now >>> >>>> >>>> Is this possible? I noticed $rspec_mocks.reset_all, but obviously >>>> I don't >>>> want everything to go away, just this one stub. >>>> >>>> >>>> -Adam >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From scott at railsnewbie.com Mon Jul 13 14:17:26 2009 From: scott at railsnewbie.com (Scott Taylor) Date: Mon, 13 Jul 2009 14:17:26 -0400 Subject: [rspec-users] Stubbing out Time correctly In-Reply-To: <978F1EF0-C1BA-4979-949F-0B8CE4B2C314@railsnewbie.com> References: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> <5CDF941C-3FC3-4DC4-8709-638F8FDBA539@railsnewbie.com> <8d18f1f80907131032v7a0bcaddw573be5777cf4dd28@mail.gmail.com> <92D9F3B1-437D-45A7-93F2-029BBC56C596@railsnewbie.com> <8AFC496E-83EA-4700-8581-E95E365B054F@railsnewbie.com> <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> <978F1EF0-C1BA-4979-949F-0B8CE4B2C314@railsnewbie.com> Message-ID: <1850DB62-3AAD-45E4-A36D-35AB98BD2AED@railsnewbie.com> On Jul 13, 2009, at 2:15 PM, Scott Taylor wrote: > > On Jul 13, 2009, at 2:12 PM, David Chelimsky wrote: > >> On Mon, Jul 13, 2009 at 1:04 PM, Scott >> Taylor wrote: >>> >>> On Jul 13, 2009, at 1:46 PM, Scott Taylor wrote: >>> >>> On Jul 13, 2009, at 1:32 PM, Adam Anderson wrote: >>> >>> Thanks for the reply, Scott. >>> >>> What you describe is what is currently being done. >>> >>> now = Time.now >>> Time.stub!(:now).and_return(foo_time) >>> do_stuff >>> Time.stub!(:now).and_return(now) >>> >>> However, this is not returning the actual time it is only >>> returning the time >>> set on the first line. I need Time.now to return to its original >>> behavior >>> (i.e., returning the current, actual time). >>> >>> Well, every time you create a stub, rspec aliased the method >>> before it >>> destroys it: >>> http://gist.github.com/1ed96143092a02cb727b >>> It's ugly, but it will work. >>> >>> Here's another solution, without needing to resort to the library: >>> http://gist.github.com/e573cb79073805a632c9 >>> Of course, this solution only works if you don't have other stubs >>> on Time. >> >> It also only works as long as that method keeps its name :) That >> method is explicitly :nodoc:'d which indicates that it is an internal >> method and therefore subject to change. Not saying I plan to change >> it, just that of now it is not a public API. >> >> That said, this thread suggests that we need a public API for >> resetting partial stubs like this. I'm thinking something more >> intention revealing like tear_down_rspec_stubs or something like >> that. >> Thoughts? Recommendations? > > I'd like unstub! to match stub!. > > Might also consider renaming rspec_reset, or making it part of the > public api. BTW, I'd also be willing to contribute a patch for unstub!, if it is so desired. Scott > > Scott > > >> >>> Scott >>> >>> >>> You might also want to check out this library: >>> http://github.com/notahat/time_travel/tree/master >>> Scott >>> >>> >>> -Adam >>> >>> On Mon, Jul 13, 2009 at 9:58 AM, Scott Taylor >>> wrote: >>>> >>>> On Jul 13, 2009, at 12:09 PM, Adam Anderson wrote: >>>> >>>>> I can't seem to find a good way to do this. If I stub out >>>>> Time.now in one >>>>> of my specs but need to return it to its original functionality >>>>> then can I >>>>> remove the stub? >>>>> >>>>> So I'd like to say something like: >>>>> Time.stub!(:now).and_return(foo_time) >>>>> Time.now # => foo_time >>>>> Time.unstub!(:now) >>>>> Time.now # => whatever time it actually is >>>> >>>> >>>> Capture it before you stub it: >>>> >>>> real_now= Time.now >>>> >>>> Time.stub!(:now).and_return a_later_time >>>> >>>> Time.stub!(:now).and_return real_now >>>> >>>>> >>>>> Is this possible? I noticed $rspec_mocks.reset_all, but >>>>> obviously I don't >>>>> want everything to go away, just this one stub. >>>>> >>>>> >>>>> -Adam >>>>> _______________________________________________ >>>>> rspec-users mailing list >>>>> rspec-users at rubyforge.org >>>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From matt at mattwynne.net Mon Jul 13 14:20:23 2009 From: matt at mattwynne.net (Matt Wynne) Date: Mon, 13 Jul 2009 19:20:23 +0100 Subject: [rspec-users] Stubbing out Time correctly In-Reply-To: <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> References: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> <5CDF941C-3FC3-4DC4-8709-638F8FDBA539@railsnewbie.com> <8d18f1f80907131032v7a0bcaddw573be5777cf4dd28@mail.gmail.com> <92D9F3B1-437D-45A7-93F2-029BBC56C596@railsnewbie.com> <8AFC496E-83EA-4700-8581-E95E365B054F@railsnewbie.com> <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> Message-ID: <1A480870-79C5-4289-8BD1-B257065A6741@mattwynne.net> On 13 Jul 2009, at 19:12, David Chelimsky wrote: > On Mon, Jul 13, 2009 at 1:04 PM, Scott Taylor > wrote: >> >> On Jul 13, 2009, at 1:46 PM, Scott Taylor wrote: >> >> On Jul 13, 2009, at 1:32 PM, Adam Anderson wrote: >> >> Thanks for the reply, Scott. >> >> What you describe is what is currently being done. >> >> now = Time.now >> Time.stub!(:now).and_return(foo_time) >> do_stuff >> Time.stub!(:now).and_return(now) >> >> However, this is not returning the actual time it is only returning >> the time >> set on the first line. I need Time.now to return to its original >> behavior >> (i.e., returning the current, actual time). >> >> Well, every time you create a stub, rspec aliased the method before >> it >> destroys it: >> http://gist.github.com/1ed96143092a02cb727b >> It's ugly, but it will work. >> >> Here's another solution, without needing to resort to the library: >> http://gist.github.com/e573cb79073805a632c9 >> Of course, this solution only works if you don't have other stubs >> on Time. > > It also only works as long as that method keeps its name :) That > method is explicitly :nodoc:'d which indicates that it is an internal > method and therefore subject to change. Not saying I plan to change > it, just that of now it is not a public API. > > That said, this thread suggests that we need a public API for > resetting partial stubs like this. I'm thinking something more > intention revealing like tear_down_rspec_stubs or something like that. > Thoughts? Recommendations? Would it be possible to get a handle to the individual stub and tear that down? stub = Time.stub!(:now).and_return(foo_time) # do stuff stub.remove Allowing you to do stuff like with_stubbed_time(foo_time) do # do stuff end > >> Scott >> >> >> You might also want to check out this library: >> http://github.com/notahat/time_travel/tree/master >> Scott >> >> >> -Adam >> >> On Mon, Jul 13, 2009 at 9:58 AM, Scott Taylor >> wrote: >>> >>> On Jul 13, 2009, at 12:09 PM, Adam Anderson wrote: >>> >>>> I can't seem to find a good way to do this. If I stub out >>>> Time.now in one >>>> of my specs but need to return it to its original functionality >>>> then can I >>>> remove the stub? >>>> >>>> So I'd like to say something like: >>>> Time.stub!(:now).and_return(foo_time) >>>> Time.now # => foo_time >>>> Time.unstub!(:now) >>>> Time.now # => whatever time it actually is >>> >>> >>> Capture it before you stub it: >>> >>> real_now= Time.now >>> >>> Time.stub!(:now).and_return a_later_time >>> >>> Time.stub!(:now).and_return real_now >>> >>>> >>>> Is this possible? I noticed $rspec_mocks.reset_all, but obviously >>>> I don't >>>> want everything to go away, just this one stub. >>>> >>>> >>>> -Adam >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users cheers, Matt Wynne http://mattwynne.net +447974 430184 From matt at mattwynne.net Mon Jul 13 14:21:40 2009 From: matt at mattwynne.net (Matt Wynne) Date: Mon, 13 Jul 2009 19:21:40 +0100 Subject: [rspec-users] Stubbing out Time correctly In-Reply-To: <1850DB62-3AAD-45E4-A36D-35AB98BD2AED@railsnewbie.com> References: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> <5CDF941C-3FC3-4DC4-8709-638F8FDBA539@railsnewbie.com> <8d18f1f80907131032v7a0bcaddw573be5777cf4dd28@mail.gmail.com> <92D9F3B1-437D-45A7-93F2-029BBC56C596@railsnewbie.com> <8AFC496E-83EA-4700-8581-E95E365B054F@railsnewbie.com> <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> <978F1EF0-C1BA-4979-949F-0B8CE4B2C314@railsnewbie.com> <1850DB62-3AAD-45E4-A36D-35AB98BD2AED@railsnewbie.com> Message-ID: On 13 Jul 2009, at 19:17, Scott Taylor wrote: > > On Jul 13, 2009, at 2:15 PM, Scott Taylor wrote: > >> >> On Jul 13, 2009, at 2:12 PM, David Chelimsky wrote: >> >>> On Mon, Jul 13, 2009 at 1:04 PM, Scott >>> Taylor wrote: >>>> >>>> On Jul 13, 2009, at 1:46 PM, Scott Taylor wrote: >>>> >>>> On Jul 13, 2009, at 1:32 PM, Adam Anderson wrote: >>>> >>>> Thanks for the reply, Scott. >>>> >>>> What you describe is what is currently being done. >>>> >>>> now = Time.now >>>> Time.stub!(:now).and_return(foo_time) >>>> do_stuff >>>> Time.stub!(:now).and_return(now) >>>> >>>> However, this is not returning the actual time it is only >>>> returning the time >>>> set on the first line. I need Time.now to return to its original >>>> behavior >>>> (i.e., returning the current, actual time). >>>> >>>> Well, every time you create a stub, rspec aliased the method >>>> before it >>>> destroys it: >>>> http://gist.github.com/1ed96143092a02cb727b >>>> It's ugly, but it will work. >>>> >>>> Here's another solution, without needing to resort to the library: >>>> http://gist.github.com/e573cb79073805a632c9 >>>> Of course, this solution only works if you don't have other stubs >>>> on Time. >>> >>> It also only works as long as that method keeps its name :) That >>> method is explicitly :nodoc:'d which indicates that it is an >>> internal >>> method and therefore subject to change. Not saying I plan to change >>> it, just that of now it is not a public API. >>> >>> That said, this thread suggests that we need a public API for >>> resetting partial stubs like this. I'm thinking something more >>> intention revealing like tear_down_rspec_stubs or something like >>> that. >>> Thoughts? Recommendations? >> >> I'd like unstub! to match stub!. >> >> Might also consider renaming rspec_reset, or making it part of the >> public api. > > > BTW, I'd also be willing to contribute a patch for unstub!, if it is > so desired. Given that we can stack up stubs and should_receive, is there a danger it could get confusing as to exactly which stub you want to unstub? cheers, Matt Wynne http://mattwynne.net +447974 430184 From lists at ruby-forum.com Tue Jul 14 05:49:16 2009 From: lists at ruby-forum.com (Zhenning Guan) Date: Tue, 14 Jul 2009 11:49:16 +0200 Subject: [rspec-users] why should_receive doesn't work, but stub does. Message-ID: <195840a2d9c60c273a709cbfda4ee892@ruby-forum.com> before(:each) do @myinstance = GetHtml.new end it "should be a Hyricot" do hy = open('test.html') {|f| Hpricot f} @myinstance.should_receive(:find_html).with("file:///test_url").and_return(hy) end ++++++++++++++++++++++++++++++++++ it "should be a Hyricot" do hy = open('test.html') {|f| Hpricot f} @myinstance.stub!(:find_html).with("file:///test_url").and_return(hy) end ++++++++++++++++++++++++++++++++++ expected ................ once, but received it 0 times I have a find_html method in my GetHtml class, but why can't should_receive run successfuly? but use stub!, it can. -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Tue Jul 14 06:35:44 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 14 Jul 2009 05:35:44 -0500 Subject: [rspec-users] why should_receive doesn't work, but stub does. In-Reply-To: <195840a2d9c60c273a709cbfda4ee892@ruby-forum.com> References: <195840a2d9c60c273a709cbfda4ee892@ruby-forum.com> Message-ID: <57c63afe0907140335s3678b018vbb7b217f5f1f994a@mail.gmail.com> On Tue, Jul 14, 2009 at 4:49 AM, Zhenning Guan wrote: > ?before(:each) do > ? ? @myinstance = GetHtml.new > ?end > > ?it "should be a Hyricot" do > ? ?hy = open('test.html') {|f| Hpricot f} > ? ?@myinstance.should_receive(:find_html).with("file:///test_url").and_return(hy) > > ?end > ++++++++++++++++++++++++++++++++++ > ?it "should be a Hyricot" do > ? ?hy = open('test.html') {|f| Hpricot f} > ? ?@myinstance.stub!(:find_html).with("file:///test_url").and_return(hy) > > ?end > ++++++++++++++++++++++++++++++++++ > > expected ................ once, but received it 0 times > > I have a find_html method in my GetHtml class, but why can't > should_receive run successfuly? but use stub!, it can. Both stub! and should_receive are intended to run *before* the event in an example. In both examples, nothing happens after they are run. The reason that the stub! appears to be running successfully is that there are no expectations being set anywhere. In essence, that example doesn't actually test anything The should_receive example expects a call to find_html *after* the expectation is set. That never happens, so it fails. In neither case is there any interaction with the instance of GetHtml other than to set the stub/expectation on it. What is it that you're trying to specify? From lists at ruby-forum.com Tue Jul 14 22:59:22 2009 From: lists at ruby-forum.com (Zhenning Guan) Date: Wed, 15 Jul 2009 04:59:22 +0200 Subject: [rspec-users] why should_receive failure? Message-ID: <0944ce655a8c7a9e821169d771029661@ruby-forum.com> class PeepCode def awesome "awesome" end end describe PeepCode do it "should fuck" do PeepCode.new.should_receive(:awesome).and_return("awesome") end end ----------------------------------------- Spec::Mocks::MockExpectationError in 'PeepCode should fuck' # expected :awesome with (any args) once, but received it 0 times ./simple_spec.rb:13: Finished in 0.006159 seconds 1 example, 1 failure ------------------------------------------ what's wrong with my code? -- Posted via http://www.ruby-forum.com/. From jim at saturnflyer.com Tue Jul 14 23:36:54 2009 From: jim at saturnflyer.com (Jim Gay) Date: Tue, 14 Jul 2009 23:36:54 -0400 Subject: [rspec-users] why should_receive failure? In-Reply-To: <0944ce655a8c7a9e821169d771029661@ruby-forum.com> References: <0944ce655a8c7a9e821169d771029661@ruby-forum.com> Message-ID: <1035E6E6-ED12-4655-9EC6-3A49E3801BD8@saturnflyer.com> On Jul 14, 2009, at 10:59 PM, Zhenning Guan wrote: > > class PeepCode > > def awesome > "awesome" > end > > end > > describe PeepCode do > > it "should fuck" do peep = PeepCode.new peep.should_receive(:awesome).and_return("awesome") peep.awesome #this completes the expectation above > # PeepCode.new.should_receive(:awesome).and_return("awesome") > end > end > > > ----------------------------------------- > Spec::Mocks::MockExpectationError in 'PeepCode should fuck' > # expected :awesome with (any args) once, but > received it 0 times > ./simple_spec.rb:13: > > Finished in 0.006159 seconds > > 1 example, 1 failure > > ------------------------------------------ > > > what's wrong with my code? I think you misunderstand "should_receive". It doesn't actually call the method you're describing; it creates an expectation. So your spec says that something should happen, and then you never called the method, so it "received it 0 times" This is a bad example though. Your code seems to say that this is the spec: describe PeepCode, "awesome" do it "should return the string 'awesome'" do PeepCode.new.awesome.should == 'awesome' end end "should_receive" says that in the test which is being run, the object should receive that method call... for example: class PeepCode def awesome totally_rad end def totally_rad "righteous" end end and your spec: describe PeepCode, "awesome" do it "should call the totally_rad method" do peep = PeepCode.new peep.should_receive(:totally_rad).and_return(...whatever...) peep.awesome end it "should return the string 'righteous'" do PeepCode.new.awesome.should == 'righteous' end end I haven't tried any of that, but it should work. Jim Gay http://www.saturnflyer.com From dchelimsky at gmail.com Tue Jul 14 23:37:20 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 14 Jul 2009 22:37:20 -0500 Subject: [rspec-users] why should_receive failure? In-Reply-To: <0944ce655a8c7a9e821169d771029661@ruby-forum.com> References: <0944ce655a8c7a9e821169d771029661@ruby-forum.com> Message-ID: <57c63afe0907142037l649a6200o1993006cde2afde@mail.gmail.com> On Tue, Jul 14, 2009 at 9:59 PM, Zhenning Guan wrote: > > class PeepCode > ?def awesome > ? ?"awesome" > ?end > end > > describe PeepCode do > ?it "should fuck" do > ? PeepCode.new.should_receive(:awesome).and_return("awesome") > ?end > end > > ----------------------------------------- > Spec::Mocks::MockExpectationError in 'PeepCode should fuck' > # expected :awesome with (any args) once, but > received it 0 times > ./simple_spec.rb:13: > > Finished in 0.006159 seconds > > 1 example, 1 failure > > ------------------------------------------ > > > what's wrong with my code? You asked a similar question yesterday and I tried to explain. Did you not receive my response? should_receive sets an expectation that a subsequent event will cause the PeepCode object to receive the :awesome message. There is no code after that expectation is set, so the message is never received, and the expectation fails. That's exactly what the error message is telling you: expected :awesome with (any args) once, but received it 0 times. If you're trying to specify that the PeepCode object should return "awesome" when you call the awesome() method, then the example should look like this: describe PeepCode, "#awesome" do it "should return 'awesome'" do peepcode = PeepCode.new peepcode.awesome.should == "awesome" end end There's no need to set a message expectation (e.g. mock) here. HTH, David ps - I think it's OK to say "fuck" on this list, if that's what you really mean, but I don't really understand what you're getting at with the expectation that PeepCode should fuck. Perhaps you might consider being a bit more careful about the examples you send. From dchelimsky at gmail.com Wed Jul 15 00:15:39 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 14 Jul 2009 23:15:39 -0500 Subject: [rspec-users] Stubbing out Time correctly In-Reply-To: <1850DB62-3AAD-45E4-A36D-35AB98BD2AED@railsnewbie.com> References: <8d18f1f80907130909h2f7f3aefgb5a03ba21c2ce849@mail.gmail.com> <5CDF941C-3FC3-4DC4-8709-638F8FDBA539@railsnewbie.com> <8d18f1f80907131032v7a0bcaddw573be5777cf4dd28@mail.gmail.com> <92D9F3B1-437D-45A7-93F2-029BBC56C596@railsnewbie.com> <8AFC496E-83EA-4700-8581-E95E365B054F@railsnewbie.com> <57c63afe0907131112y336cea53j1893404b4e83ecb7@mail.gmail.com> <978F1EF0-C1BA-4979-949F-0B8CE4B2C314@railsnewbie.com> <1850DB62-3AAD-45E4-A36D-35AB98BD2AED@railsnewbie.com> Message-ID: <57c63afe0907142115s29daeb52t94e9a37322da8464@mail.gmail.com> On Mon, Jul 13, 2009 at 1:17 PM, Scott Taylor wrote: > > On Jul 13, 2009, at 2:15 PM, Scott Taylor wrote: > >> >> On Jul 13, 2009, at 2:12 PM, David Chelimsky wrote: >> >>> On Mon, Jul 13, 2009 at 1:04 PM, Scott Taylor >>> wrote: >>>> >>>> On Jul 13, 2009, at 1:46 PM, Scott Taylor wrote: >>>> >>>> On Jul 13, 2009, at 1:32 PM, Adam Anderson wrote: >>>> >>>> Thanks for the reply, Scott. >>>> >>>> What you describe is what is currently being done. >>>> >>>> now = Time.now >>>> Time.stub!(:now).and_return(foo_time) >>>> do_stuff >>>> Time.stub!(:now).and_return(now) >>>> >>>> However, this is not returning the actual time it is only returning the >>>> time >>>> set on the first line. I need Time.now to return to its original >>>> behavior >>>> (i.e., returning the current, actual time). >>>> >>>> Well, every time you create a stub, rspec aliased the method before it >>>> destroys it: >>>> http://gist.github.com/1ed96143092a02cb727b >>>> It's ugly, but it will work. >>>> >>>> Here's another solution, without needing to resort to the library: >>>> http://gist.github.com/e573cb79073805a632c9 >>>> Of course, this solution only works if you don't have other stubs on >>>> Time. >>> >>> It also only works as long as that method keeps its name :) That >>> method is explicitly :nodoc:'d which indicates that it is an internal >>> method and therefore subject to change. Not saying I plan to change >>> it, just that of now it is not a public API. >>> >>> That said, this thread suggests that we need a public API for >>> resetting partial stubs like this. I'm thinking something more >>> intention revealing like tear_down_rspec_stubs or something like that. >>> Thoughts? Recommendations? >> >> I'd like unstub! to match stub!. >> >> Might also consider renaming rspec_reset, or making it part of the public >> api. > > > BTW, I'd also be willing to contribute a patch for unstub!, if it is so > desired. FYI - Scott submitted this patch and I just merged it, so you should now be able to unstub! Time.now mid-example. http://github.com/dchelimsky/rspec/commit/281dc54d4556fed35b62e2f1b86a13e443dcd6e8 Cheers (and thanks, Scott!), David > > Scott > > >> >> Scott >> >> >>> >>>> Scott >>>> >>>> >>>> You might also want to check out this library: >>>> http://github.com/notahat/time_travel/tree/master >>>> Scott >>>> >>>> >>>> -Adam >>>> >>>> On Mon, Jul 13, 2009 at 9:58 AM, Scott Taylor >>>> wrote: >>>>> >>>>> On Jul 13, 2009, at 12:09 PM, Adam Anderson wrote: >>>>> >>>>>> I can't seem to find a good way to do this. If I stub out Time.now in >>>>>> one >>>>>> of my specs but need to return it to its original functionality then >>>>>> can I >>>>>> remove the stub? >>>>>> >>>>>> So I'd like to say something like: >>>>>> Time.stub!(:now).and_return(foo_time) >>>>>> Time.now # => foo_time >>>>>> Time.unstub!(:now) >>>>>> Time.now # => whatever time it actually is >>>>> >>>>> >>>>> Capture it before you stub it: >>>>> >>>>> real_now= Time.now >>>>> >>>>> Time.stub!(:now).and_return a_later_time >>>>> >>>>> Time.stub!(:now).and_return real_now >>>>> >>>>>> >>>>>> Is this possible? I noticed $rspec_mocks.reset_all, but obviously I >>>>>> don't >>>>>> want everything to go away, just this one stub. >>>>>> >>>>>> >>>>>> -Adam >>>>>> _______________________________________________ >>>>>> rspec-users mailing list >>>>>> rspec-users at rubyforge.org >>>>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>>> >>>>> _______________________________________________ >>>>> rspec-users mailing list >>>>> rspec-users at rubyforge.org >>>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> 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 julian at leviston.net Tue Jul 14 23:32:05 2009 From: julian at leviston.net (Julian Leviston) Date: Wed, 15 Jul 2009 13:32:05 +1000 Subject: [rspec-users] why should_receive failure? In-Reply-To: <0944ce655a8c7a9e821169d771029661@ruby-forum.com> References: <0944ce655a8c7a9e821169d771029661@ruby-forum.com> Message-ID: <6741D6E2-B052-412F-BE78-27547AD37E0D@leviston.net> because "Peepcode.new" doesn't receive the "awesome" message. like it says... Julian. On 15/07/2009, at 12:59 PM, Zhenning Guan wrote: > > class PeepCode > > def awesome > "awesome" > end > > end > > describe PeepCode do > > it "should fuck" do > PeepCode.new.should_receive(:awesome).and_return("awesome") > end > end > > > ----------------------------------------- > Spec::Mocks::MockExpectationError in 'PeepCode should fuck' > # expected :awesome with (any args) once, but > received it 0 times > ./simple_spec.rb:13: > > Finished in 0.006159 seconds > > 1 example, 1 failure > > ------------------------------------------ > > > what's wrong with my code? > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From felipero.maillist at gmail.com Wed Jul 15 15:57:57 2009 From: felipero.maillist at gmail.com (Felipe Rodrigues 02) Date: Wed, 15 Jul 2009 12:57:57 -0700 (PDT) Subject: [rspec-users] Problems installing the rspec and rspec-rails gems In-Reply-To: <8642DBE8-C751-4785-B620-EEED4638212E@gmail.com> References: <1be6e98e959cd635453c4d2b3ad74376@ruby-forum.com> <2c7e61990904041715p54aaabffrc367d1eb4e1e7840@mail.gmail.com> <09ff70a32694aea73a8a9884f92eb6cb@ruby-forum.com> <8642DBE8-C751-4785-B620-EEED4638212E@gmail.com> Message-ID: <24504741.post@talk.nabble.com> I just trying to use cucumber in a rails project following the RSpec book from Pragmatic Programmers, but I am seeing the same error. The --no-rdoc didn't worked. Don't know where else to look for a solution. Any ideas on this one? David Chelimsky-2 wrote: > > Hey Mike, > > Looks like the problem is installing the rdoc. Try installing with the > --no-rdoc option. > > Cheers, > David > > Sent from my iPhone > > On Apr 4, 2009, at 7:52 PM, Mike Williams wrote: > >> Pat Maddox wrote: >>> The rspec gem did install successfully, not sure what those doc >>> warnings are. Did you install rspec-rails as well? >>> >>> Pat >> >> >> I get the same output with rspec-rails when I try to install it. >> >> When I try to run cucumber feature -n I get: >> >> Missing these required gems: >> rpsec >= 1.2.2 >> rpsec-rails >= 1.2.2 >> >> You're running: >> ruby 1.8.6.114 at >> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby >> rubygems 1.3.1 at /Users/mike/.gem/ruby/1.8, /Library/Ruby/Gems/1.8, >> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ >> gems/1.8 >> -- >> Posted via http://www.ruby-forum.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 > > ----- Felipe Rodrigues http://www.fratech.net -- View this message in context: http://www.nabble.com/Problems-installing-the-rspec-and-rspec-rails-gems-tp22889148p24504741.html Sent from the rspec-users mailing list archive at Nabble.com. From felipero.maillist at gmail.com Wed Jul 15 16:25:26 2009 From: felipero.maillist at gmail.com (Felipe Rodrigues 02) Date: Wed, 15 Jul 2009 13:25:26 -0700 (PDT) Subject: [rspec-users] Cucumber + Rspec problem: Missing these required gems: Message-ID: <24505136.post@talk.nabble.com> Hi all, I'm stuck with this error. I've just created a rails project and installed rspec, rspec-rails, cucumber and webrat as gems. So put in my environment.rb the following: config.gem 'rspec-rails', :lib => false config.gem 'rspec', :lib => false config.gem 'cucumber' config.gem 'webrat' Then I shot in my shell script/generate rspec followed by script/generate cucumber Basically following this tutorial: http://www.ultrasaurus.com/sarahblog/2008/12/rails-2-day-3-behavior-driven-development/ So, I create a tasks_list.feature inside my features directory. Also a tasks_list_steps.rb on my features/steps_definitions directory. When I run "script/cucumber features" or "rake features" or even "cucumber features" I get the following: ============================================ Missing these required gems: rspec >= 1.2.6 rspec-rails >= 1.2.6 You're running: ruby 1.8.7.72 at /usr/bin/ruby1.8 rubygems 1.3.4 at /home/felipe/.gem/ruby/1.8, /usr/lib/ruby/gems/1.8 Run `rake gems:install` to install the missing gems. ====================================================== Any ideas about how fix that? Thanks, Felipe ----- Felipe Rodrigues http://www.fratech.net -- View this message in context: http://www.nabble.com/Cucumber-%2B-Rspec-problem%3A-Missing-these-required-gems%3A-tp24505136p24505136.html Sent from the rspec-users mailing list archive at Nabble.com. From felipero.maillist at gmail.com Wed Jul 15 17:31:12 2009 From: felipero.maillist at gmail.com (Felipe Rodrigues 02) Date: Wed, 15 Jul 2009 14:31:12 -0700 (PDT) Subject: [rspec-users] Cucumber + Rspec problem: Missing these required gems: In-Reply-To: <24505136.post@talk.nabble.com> References: <24505136.post@talk.nabble.com> Message-ID: <24506119.post@talk.nabble.com> Looks like this problem only happens in the rails edge. Felipe Rodrigues 02 wrote: > > Hi all, > > I'm stuck with this error. I've just created a rails project and installed > rspec, rspec-rails, cucumber and webrat as gems. So put in my > environment.rb the following: > > config.gem 'rspec-rails', :lib => false > config.gem 'rspec', :lib => false > config.gem 'cucumber' > config.gem 'webrat' > > Then I shot in my shell > > script/generate rspec > > followed by > > script/generate cucumber > > Basically following this tutorial: > http://www.ultrasaurus.com/sarahblog/2008/12/rails-2-day-3-behavior-driven-development/ > > So, I create a tasks_list.feature inside my features directory. Also a > tasks_list_steps.rb on my features/steps_definitions directory. > > When I run "script/cucumber features" or "rake features" or even "cucumber > features" I get the following: > ============================================ > Missing these required gems: > rspec >= 1.2.6 > rspec-rails >= 1.2.6 > > You're running: > ruby 1.8.7.72 at /usr/bin/ruby1.8 > rubygems 1.3.4 at /home/felipe/.gem/ruby/1.8, /usr/lib/ruby/gems/1.8 > > Run `rake gems:install` to install the missing gems. > ====================================================== > > Any ideas about how fix that? > > Thanks, > > Felipe > ----- Felipe Rodrigues http://www.fratech.net -- View this message in context: http://www.nabble.com/Cucumber-%2B-Rspec-problem%3A-Missing-these-required-gems%3A-tp24505136p24506119.html Sent from the rspec-users mailing list archive at Nabble.com. From leogodin217 at gmail.com Wed Jul 15 17:29:29 2009 From: leogodin217 at gmail.com (Leo) Date: Wed, 15 Jul 2009 14:29:29 -0700 (PDT) Subject: [rspec-users] testing controller instance variables Message-ID: HI All, I have a controller with the following in it: def index @purchase_requests = PurchaseRequest.find(:all) @num_found = @purchase_requests.size end I want to test that @num_found is getting the number of purchase requests. This is what I've tried: it "should count the number of purchase requests found" do @purchase_requests.stub!(:size).and_return 3 @num_found.should_equal(3) get :index end However, I Get This Error: NoMethodError in 'PurchaseRequestsController Get index should count the number of purchase requests found' You have a nil object when you didn't expect it! The error occurred while evaluating nil.should_equal ./spec/controllers/purchase_requests_controller_spec.rb:21: It seems that rspec wants me to stub the instance variable. If I do that then I don't think I'd actually be testing anything because I'd create the stub in the test and verify that the stub I created is there. I want to make sure that the test will fail if I delete the line in the actual controller or change the name. Can someone show me how to do this? thanks in advance. Leo From dchelimsky at gmail.com Wed Jul 15 18:21:38 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 15 Jul 2009 17:21:38 -0500 Subject: [rspec-users] Should deleting code require failing specs? In-Reply-To: <8d18f1f80907151503p5ce63890k470f56f663d28190@mail.gmail.com> References: <8d18f1f80907151503p5ce63890k470f56f663d28190@mail.gmail.com> Message-ID: <57c63afe0907151521o72c6a0e6ye6e2e26bdeef15b8@mail.gmail.com> On Wed, Jul 15, 2009 at 5:03 PM, Adam Anderson wrote: > Sometimes when features are asked to be removed it doesn't make sense to > specify that they shouldn't be there. It seems to me that removing something > from a tested app should not entail writing a failing spec for that change. > I'm curious if people have different opinions or insights on this. > > -Adam I'd generally not spec the absence of a pre-existing feature, but I think it depends on the nature of the app and the associated risk of clients (human or other systems) trying to access services that no longer exist. From dchelimsky at gmail.com Wed Jul 15 19:09:36 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 15 Jul 2009 18:09:36 -0500 Subject: [rspec-users] Routes helpers work differently from helper examples compared to page rendering In-Reply-To: <1c3c86ea-4145-4314-877d-b6a71779a4b5@d9g2000prh.googlegroups.com> References: <358a910b-edea-4dce-a745-26a474f31647@f20g2000prn.googlegroups.com> <57c63afe0907041553s223a0c21o5023428dc29392d8@mail.gmail.com> <1c3c86ea-4145-4314-877d-b6a71779a4b5@d9g2000prh.googlegroups.com> Message-ID: <57c63afe0907151609m2dd4333ds689004bd4158ed94@mail.gmail.com> On Wed, Jul 8, 2009 at 2:03 PM, Ed Ruder wrote: > I don't think that ticket applies to my problem--as I read it, > https://rspec.lighthouseapp.com/projects/5645/tickets/843 is about > enhancing RSpec with additional routing tests/specs. > > My issue is that in the context of a helper spec, when calling route > helpers (_url/_path methods) from a helper, a different Rails code > path is executed, giving different results in some cases. ?This means > that sometimes your helper specs pass when the helper "in the wild" > doesn't do what's spec'd--you cannot trust your specs! > > Thoughts? Seems like this is an rspec bug. Please file a ticket at http://rspec.lighthouseapp.com. Thanks, David > > Ed > > On Jul 4, 3:53?pm, David Chelimsky wrote: >> On Sat, Jul 4, 2009 at 4:40 PM, Ed Ruder wrote: >> > I have a helper that calls a Rails route _url method. In the context >> > of a helper spec file, the route helper method ends up calling url_for >> > in rails/actionpack/lib/action_controller/base.rb. In the context of >> > rendering a page, the route helper method calls url_for in rails/ >> > actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, >> > they can return different results, as I found out. >> >> > Specifically, ActionController::Base#url_for doesn't do html escaping-- >> > ActionView::Helpers::UrlHelper#url_for does, and has an additional >> > parameter, :escape => true|false, to controller whether or not the >> > escaping is done. >> >> > This took some frustrating time to track down. >> >> > Since the code path executed by helper examples is not the same as >> > production, tests can produce invalid results. Also, some behavior >> > cannot be tested, since the code generating the results is not being >> > executed. >> >> > Is this a bug in RSpec, or is there a "correct" way to test helpers >> > that I'm missing? >> >> Not sure if this is directly related, but you may be interested in >> this ticket:https://rspec.lighthouseapp.com/projects/5645/tickets/843 >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From chris at silhouettesolutions.net Wed Jul 15 23:14:29 2009 From: chris at silhouettesolutions.net (Chris Sund) Date: Wed, 15 Jul 2009 20:14:29 -0700 (PDT) Subject: [rspec-users] noob question (trying to understand view specs vs webrat and cucumber)... Message-ID: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> Hey Everyone, I've been reading the rspec book, and I'm trying to tie everything together, but I'm a little confused. Keep in mind this is my first introduction to TDD/BDD. When it came to testing my rails app I jumped into learning rspec before I ever tried to using any of the built in test:unit stuff. I'm probably a different breed because of this, most everyone else has probably been using the stuff built into rails before ever attempting rspec or cucumber. I didn't see the need to learn something that everyone is going away from, but if it's a necessary evil please let me know. I am to the chapter talking about writing view specs, this chapter is preceded by a chapter that talks about using webrat matchers to help you test views, and uses cucumber step definitions. I'm having a problem understanding when I should use cucumber and when I should use rspec. If I start my app coding by "spec"ing my views then do I really need to use cucumber at all? Can someone clarify in "simple" terms the relationship between rspec and cucumber. I feel I understand them independently, but I'm confused on how and when I choose to use them together. I have read through the book pretty thoroughly to this point and feel I have absorbed everything there is to absorb, but my mind isn't getting this correlation. When going through the codebreaker example it had me run my cucumber stuff separate from my rspec stuff. Looking back it seems the only reason I used both was to develop a more complete test, but in fact I could use one over the other if I wanted to? That statement makes sense "to me", but I know it's probably wrong. I guess while I'm asking questions, I'll ask one more... If I want to follow the BDD mantra to the tee, then I would spec all of my views first, and use this as my starting place for adding every subsequent test after that right? After the views have been "spec"ed and are re-factored in the "Green" then I would re-factor again by adding controller methods or model information to get rid of my stub code. Am I understanding that correctly? Any information helps me learn. Thanks! Chris From tom at experthuman.com Thu Jul 16 01:34:46 2009 From: tom at experthuman.com (Tom Stuart) Date: Thu, 16 Jul 2009 06:34:46 +0100 Subject: [rspec-users] noob question (trying to understand view specs vs webrat and cucumber)... In-Reply-To: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> References: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> Message-ID: <9BD579F5-1879-4EAB-B841-D8B69DE2C31A@experthuman.com> Hi Chris, On 16 Jul 2009, at 04:14, Chris Sund wrote: > Can someone clarify in "simple" terms the relationship between rspec > and cucumber. Very short answer: RSpec is for specifying the behaviour of individual pieces of your application, ideally in isolation from all of the other pieces. The mocking framework allows you to write specs for each separate piece (e.g. each class) in a way that doesn't depend on any of the other pieces: "Assuming that pieces A and B and C behave in the way they're supposed to, piece D should behave like this...". This gives you a way to drive the actual implementation of your application in detail, at the level of individual methods doing simple jobs. Cucumber is for specifying/testing the overall behaviour of your application. Whereas RSpec exercises each individual piece in isolation, Cucumber exercises the interactions between all of the pieces, and allows you to check what happens when several such interactions happen in a particular order (e.g. a user playing a complete game of Codebreaker, or going through the complete checkout/ payment process in a web application). Whereas RSpec examples are very fine-grained and implementation-focused, Cucumber features are meant to be broad and high-level in a way that makes them meaningful to actual users of the application (or the customers who are asking for it to be built) rather than just programmers. One way of thinking about this is that your specs make all sorts of assumptions about how the rest of the system is meant to behave, and Cucumber is a way of actually checking those assumptions by running the system as a whole. Cheers, -Tom From chris at silhouettesolutions.net Thu Jul 16 01:46:02 2009 From: chris at silhouettesolutions.net (internetchris) Date: Wed, 15 Jul 2009 22:46:02 -0700 (PDT) Subject: [rspec-users] noob question (trying to understand view specs vs webrat and cucumber)... In-Reply-To: <9BD579F5-1879-4EAB-B841-D8B69DE2C31A@experthuman.com> References: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> <9BD579F5-1879-4EAB-B841-D8B69DE2C31A@experthuman.com> Message-ID: <8dcd4945-ef20-4512-8045-e6a1a146e70c@26g2000yqk.googlegroups.com> Tom, I appreciate the reply... So would I be correct in saying that I should develop all of my spec tests first, and then finish it up by running some cucumber tests? Thanks! Chris On Jul 15, 11:34?pm, Tom Stuart wrote: > Hi Chris, > > On 16 Jul 2009, at 04:14, Chris Sund wrote: > > > Can someone clarify in "simple" terms the relationship between rspec ? > > and cucumber. > > Very short answer: > > RSpec is for specifying the behaviour of individual pieces of your ? > application, ideally in isolation from all of the other pieces. The ? > mocking framework allows you to write specs for each separate piece ? > (e.g. each class) in a way that doesn't depend on any of the other ? > pieces: "Assuming that pieces A and B and C behave in the way they're ? > supposed to, piece D should behave like this...". This gives you a way ? > to drive the actual implementation of your application in detail, at ? > the level of individual methods doing simple jobs. > > Cucumber is for specifying/testing the overall behaviour of your ? > application. Whereas RSpec exercises each individual piece in ? > isolation, Cucumber exercises the interactions between all of the ? > pieces, and allows you to check what happens when several such ? > interactions happen in a particular order (e.g. a user playing a ? > complete game of Codebreaker, or going through the complete checkout/ > payment process in a web application). Whereas RSpec examples are very ? > fine-grained and implementation-focused, Cucumber features are meant ? > to be broad and high-level in a way that makes them meaningful to ? > actual users of the application (or the customers who are asking for ? > it to be built) rather than just programmers. > > One way of thinking about this is that your specs make all sorts of ? > assumptions about how the rest of the system is meant to behave, and ? > Cucumber is a way of actually checking those assumptions by running ? > the system as a whole. > > Cheers, > -Tom > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From tom at experthuman.com Thu Jul 16 02:29:33 2009 From: tom at experthuman.com (Tom Stuart) Date: Thu, 16 Jul 2009 07:29:33 +0100 Subject: [rspec-users] noob question (trying to understand view specs vs webrat and cucumber)... In-Reply-To: <8dcd4945-ef20-4512-8045-e6a1a146e70c@26g2000yqk.googlegroups.com> References: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> <9BD579F5-1879-4EAB-B841-D8B69DE2C31A@experthuman.com> <8dcd4945-ef20-4512-8045-e6a1a146e70c@26g2000yqk.googlegroups.com> Message-ID: Hi Chris, On 16 Jul 2009, at 06:46, internetchris wrote: > So would I be correct in saying that I should develop all of my spec > tests first, and then finish it up by running some cucumber tests? You can do whatever you like. This is certainly a valid way of working, and as a testing strategy it would definitely help you to catch certain kinds of bugs that you might miss with RSpec alone. However, BDD is about the design process itself, not (just) testing, and the BDD people (and the RSpec book) recommend a process where you start at the Cucumber level, i.e. think first about an overall feature that you want your application to have, and write that feature up as Cucumber scenarios. Although those scenarios will all fail (because you haven't written any code yet), they represent what you're aiming for. To get them to pass you "drop down" into RSpec and write some more detailed specs for the behaviour you think you'll need to get the Cucumber scenarios passing, and actually implement the code (to make the specs pass) as you go. When all the specs pass then the Cucumber feature will also pass unless you've forgotten something. Then you move onto writing another Cucumber feature and the design/implement cycle continues. I'm pretty sure there's a diagram of this process (concentric circles with arrows) in the first couple of chapters of the RSpec book. Cheers, -Tom From andrew at andrewtimberlake.com Thu Jul 16 02:47:34 2009 From: andrew at andrewtimberlake.com (Andrew Timberlake) Date: Thu, 16 Jul 2009 08:47:34 +0200 Subject: [rspec-users] noob question (trying to understand view specs vs webrat and cucumber)... In-Reply-To: <8dcd4945-ef20-4512-8045-e6a1a146e70c@26g2000yqk.googlegroups.com> References: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> <9BD579F5-1879-4EAB-B841-D8B69DE2C31A@experthuman.com> <8dcd4945-ef20-4512-8045-e6a1a146e70c@26g2000yqk.googlegroups.com> Message-ID: <62ad3e390907152347j788a8f69q67c0f1940c929064@mail.gmail.com> On Thu, Jul 16, 2009 at 7:46 AM, internetchris wrote: > Tom, > > I appreciate the reply... > > So would I be correct in saying that I should develop all of my spec > tests first, and then finish it up by running some cucumber tests? > > Thanks! > > Chris > Chris If you follow strict Behaviour Driven Development, then the behaviour should be defined before the code. Therefore, the Cucumber scenarios should come first. My workflow is like this: Write Cucumber scenario - It will fail because the path isn't defined Create route entry Run Cucumber - It now fails because the controller doesn't exist Create controller spec - It will fail because there's no controller Create controller Spec now fails because required model doesn't exist Write model spec - It will fail because model doesn't exist Create model Flesh out model until model spec passes Flesh out controller until controller spec passes Continue until Cucumber scenario passes Repeat until project is complete :-) I run autospec so most of the test re-runs are done automatically and therefore the process continues a lot more smoothly than it sounds. You'll notice that their are no view specs. I don't use view specs 95% of the time because Cucumber covers this (previously my view specs where just an assertion that the view was there). When theirs something very specific I need to ensure is in the view, I might add a view spec. Andrew Timberlake http://ramblingsonrails.com http://MyMvelope.com - The SIMPLE way to manage your savings From mchung at gmail.com Thu Jul 16 02:48:47 2009 From: mchung at gmail.com (Marc Chung) Date: Wed, 15 Jul 2009 23:48:47 -0700 (PDT) Subject: [rspec-users] ArgumentError during rake specs Message-ID: Hello, On a recently setup machine, a freshly checked out project started to fail in the strangest way. When executing 'rake spec', all the specs would fail because of the same ArgumentError: "wrong number of arguments (0 for 1)" This gist contains the backtrace of a single failing test: http://gist.github.com/148267 http://gist.github.com/148267 Line 180 of vendor/rails/actionpack/lib/action_controller/test_case.rb: 180 is: def setup_controller_request_and_response @request = TestRequest.new @response = TestResponse.new ... end Which doesn't take an argument at all. Has anyone seen this behavior before? Does anyone have any ideas on how to further debug this problem? I'm running Ruby 1.8.7, Rails 2.3.0, and RSpec 1.2.6 Regards, -Marc From dchelimsky at gmail.com Thu Jul 16 08:54:44 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 16 Jul 2009 07:54:44 -0500 Subject: [rspec-users] ArgumentError during rake specs In-Reply-To: References: Message-ID: <57c63afe0907160554j5fff7ebfka0ccc4044ef3c1d4@mail.gmail.com> On Thu, Jul 16, 2009 at 1:48 AM, Marc Chung wrote: > Hello, > > On a recently setup machine, a freshly checked out project started to > fail in the strangest way. When executing 'rake spec', all the specs > would fail because of the same ArgumentError: > > "wrong number of arguments (0 for 1)" > > This gist contains the backtrace of a single failing test: > http://gist.github.com/148267 > > http://gist.github.com/148267 > > Line 180 of vendor/rails/actionpack/lib/action_controller/test_case.rb: > 180 is: > > ? def setup_controller_request_and_response > ? ? ?@request = TestRequest.new > ? ? ?@response = TestResponse.new > ? ? ?... > ? end > > Which doesn't take an argument at all. > > Has anyone seen this behavior before? > Does anyone have any ideas on how to further debug this problem? > I'm running Ruby 1.8.7, Rails 2.3.0, and RSpec 1.2.6 First - Rails-2.3.0 was a release candidate, and there were some changes before it went final. So I'd recommend upgrading that to 2.3.2 (or at least 2.3.1) to start. Also, the backtrace shows that you've got rails vendored - are you absolutely sure that you've got 2.3.0 and not some arbitrary commit from whatever was edge at the moment? > > Regards, > > -Marc > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From sfeley at gmail.com Thu Jul 16 11:35:52 2009 From: sfeley at gmail.com (Stephen Eley) Date: Thu, 16 Jul 2009 11:35:52 -0400 Subject: [rspec-users] Should deleting code require failing specs? In-Reply-To: <8d18f1f80907151503p5ce63890k470f56f663d28190@mail.gmail.com> References: <8d18f1f80907151503p5ce63890k470f56f663d28190@mail.gmail.com> Message-ID: <1fb4df0907160835o3d630e30k81a859d78bb5ea8f@mail.gmail.com> On Wed, Jul 15, 2009 at 6:03 PM, Adam Anderson wrote: > Sometimes when features are asked to be removed it doesn't make sense to > specify that they shouldn't be there. It seems to me that removing something > from a tested app should not entail writing a failing spec for that change. > I'm curious if people have different opinions or insights on this. I'd think about the following: * Is the *absence* of the feature a new functional requirement? * Would the existence of the feature after it's not supposed to be there any more have an impact on users or other stakeholders? * Is removing the feature likely to be problematic for any reason? * Is it at all possible that the way you deploy code could cause the feature -- or part of the feature -- to be there when it shouldn't be? (This is usually a "yes." Even if you're doing an online app with cookie-cutter Git and Capistrano deployment, someone could forget to merge the right branch, or leave "drop table" migrations dangling, or whatever.) You want to spec things that matter. If it *matters* that something goes away in a predictable manner, and there's any possibility that it might not, it's probably worthwhile to write specs for it. If the reason you're removing it is just because no one's using it and no one cares... *shrug* Then you're not obliged to care more than anybody else does. >8-> -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From chris at silhouettesolutions.net Thu Jul 16 12:28:37 2009 From: chris at silhouettesolutions.net (internetchris) Date: Thu, 16 Jul 2009 09:28:37 -0700 (PDT) Subject: [rspec-users] noob question (trying to understand view specs vs webrat and cucumber)... In-Reply-To: <62ad3e390907152347j788a8f69q67c0f1940c929064@mail.gmail.com> References: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> <9BD579F5-1879-4EAB-B841-D8B69DE2C31A@experthuman.com> <8dcd4945-ef20-4512-8045-e6a1a146e70c@26g2000yqk.googlegroups.com> <62ad3e390907152347j788a8f69q67c0f1940c929064@mail.gmail.com> Message-ID: <22a78829-b726-4949-84a6-20c0ad12cbf3@i6g2000yqj.googlegroups.com> Very nice.... the ability to see your workflow helps me a ton. I guess I needed to see what other developers did. I have a project that I started, but then quit until I nailed down the testing. I will have to catch up on the code I have already written, but I'm grasping the cucumber and rspec correlation now. On anything new I will start with Cucumber and then to rspec to iron out the actual code. Thanks! Chris On Jul 16, 12:47?am, Andrew Timberlake wrote: > On Thu, Jul 16, 2009 at 7:46 AM, > > internetchris wrote: > > Tom, > > > I appreciate the reply... > > > So would I be correct in saying that I should develop all of my spec > > tests first, and then finish it up by running some cucumber tests? > > > Thanks! > > > Chris > > Chris > > If you follow strict Behaviour Driven Development, then the behaviour > should be defined before the code. Therefore, the Cucumber scenarios > should come first. > > My workflow is like this: > Write Cucumber scenario - It will fail because the path isn't defined > Create route entry > Run Cucumber - It now fails because the controller doesn't exist > Create controller spec - It will fail because there's no controller > Create controller > Spec now fails because required model doesn't exist > Write model spec - It will fail because model doesn't exist > Create model > Flesh out model until model spec passes > Flesh out controller until controller spec passes > Continue until Cucumber scenario passes > Repeat until project is complete :-) > > I run autospec so most of the test re-runs are done automatically and > therefore the process continues a lot more smoothly than it sounds. > You'll notice that their are no view specs. I don't use view specs 95% > of the time because Cucumber covers this (previously my view specs > where just an assertion that the view was there). > When theirs something very specific I need to ensure is in the view, I > might add a view spec. > > Andrew Timberlakehttp://ramblingsonrails.com > > http://MyMvelope.com- The SIMPLE way to manage your savings > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From mchung at gmail.com Thu Jul 16 16:34:01 2009 From: mchung at gmail.com (Marc Chung) Date: Thu, 16 Jul 2009 13:34:01 -0700 (PDT) Subject: [rspec-users] ArgumentError during rake specs In-Reply-To: <57c63afe0907160554j5fff7ebfka0ccc4044ef3c1d4@mail.gmail.com> References: <57c63afe0907160554j5fff7ebfka0ccc4044ef3c1d4@mail.gmail.com> Message-ID: David, Thank you, that did it. I vendored rails-2.3.2 and all the specs started working again. I tried it out with 2.3.3, but I ran into a single failing spec (SystemStackError: stack level too deep). I'm good with 2.3.2. -Marc On Jul 16, 5:54?am, David Chelimsky wrote: > On Thu, Jul 16, 2009 at 1:48 AM, Marc Chung wrote: > > Hello, > > > On a recently setup machine, a freshly checked out project started to > > fail in the strangest way. When executing 'rake spec', all the specs > > would fail because of the same ArgumentError: > > > "wrong number of arguments (0 for 1)" > > > This gist contains the backtrace of a single failing test: > >http://gist.github.com/148267 > > >http://gist.github.com/148267 > > > Line 180 of vendor/rails/actionpack/lib/action_controller/test_case.rb: > > 180 is: > > > ? def setup_controller_request_and_response > > ? ? ?@request = TestRequest.new > > ? ? ?@response = TestResponse.new > > ? ? ?... > > ? end > > > Which doesn't take an argument at all. > > > Has anyone seen this behavior before? > > Does anyone have any ideas on how to further debug this problem? > > I'm running Ruby 1.8.7, Rails 2.3.0, and RSpec 1.2.6 > > First - Rails-2.3.0 was a release candidate, and there were some > changes before it went final. So I'd recommend upgrading that to 2.3.2 > (or at least 2.3.1) to start. > > Also, the backtrace shows that you've got rails vendored - are you > absolutely sure that you've got 2.3.0 and not some arbitrary commit > from whatever was edge at the moment? > > > > > Regards, > > > -Marc > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Thu Jul 16 16:40:48 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 16 Jul 2009 15:40:48 -0500 Subject: [rspec-users] ArgumentError during rake specs In-Reply-To: References: <57c63afe0907160554j5fff7ebfka0ccc4044ef3c1d4@mail.gmail.com> Message-ID: <57c63afe0907161340i382c8a5epae8a391b52d52e6c@mail.gmail.com> $ gem q -rn rails | grep ^rails* rails (2.3.2) What's 2.3.3? On Thu, Jul 16, 2009 at 3:34 PM, Marc Chung wrote: > David, > > Thank you, that did it. > > I vendored rails-2.3.2 and all the specs started working again. ?I > tried it out with 2.3.3, but I ran into a single failing spec > (SystemStackError: stack level too deep). > > I'm good with 2.3.2. > > -Marc > > > On Jul 16, 5:54?am, David Chelimsky wrote: >> On Thu, Jul 16, 2009 at 1:48 AM, Marc Chung wrote: >> > Hello, >> >> > On a recently setup machine, a freshly checked out project started to >> > fail in the strangest way. When executing 'rake spec', all the specs >> > would fail because of the same ArgumentError: >> >> > "wrong number of arguments (0 for 1)" >> >> > This gist contains the backtrace of a single failing test: >> >http://gist.github.com/148267 >> >> >http://gist.github.com/148267 >> >> > Line 180 of vendor/rails/actionpack/lib/action_controller/test_case.rb: >> > 180 is: >> >> > ? def setup_controller_request_and_response >> > ? ? ?@request = TestRequest.new >> > ? ? ?@response = TestResponse.new >> > ? ? ?... >> > ? end >> >> > Which doesn't take an argument at all. >> >> > Has anyone seen this behavior before? >> > Does anyone have any ideas on how to further debug this problem? >> > I'm running Ruby 1.8.7, Rails 2.3.0, and RSpec 1.2.6 >> >> First - Rails-2.3.0 was a release candidate, and there were some >> changes before it went final. So I'd recommend upgrading that to 2.3.2 >> (or at least 2.3.1) to start. >> >> Also, the backtrace shows that you've got rails vendored - are you >> absolutely sure that you've got 2.3.0 and not some arbitrary commit >> from whatever was edge at the moment? >> >> >> >> > Regards, >> >> > -Marc >> > _______________________________________________ >> > rspec-users mailing list >> > rspec-us... at rubyforge.org >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From matt at mattwynne.net Thu Jul 16 18:06:28 2009 From: matt at mattwynne.net (Matt Wynne) Date: Thu, 16 Jul 2009 23:06:28 +0100 Subject: [rspec-users] Should deleting code require failing specs? In-Reply-To: <8d18f1f80907151503p5ce63890k470f56f663d28190@mail.gmail.com> References: <8d18f1f80907151503p5ce63890k470f56f663d28190@mail.gmail.com> Message-ID: On 15 Jul 2009, at 23:03, Adam Anderson wrote: > Sometimes when features are asked to be removed it doesn't make > sense to specify that they shouldn't be there. It seems to me that > removing something from a tested app should not entail writing a > failing spec for that change. I'm curious if people have different > opinions or insights on this. I think it depends on whether there's a chance the change you're making to remove the feature could regress somehow. If you feel the need to hem that potential regression in with a test then do it. If you're just chopping off loose code that nobody cares about anymore, chop if off and move on. cheers, Matt +447974 430184 matt at mattwynne.net http://mattwynne.net From matt at mattwynne.net Thu Jul 16 18:10:42 2009 From: matt at mattwynne.net (Matt Wynne) Date: Thu, 16 Jul 2009 23:10:42 +0100 Subject: [rspec-users] noob question (trying to understand view specs vs webrat and cucumber)... In-Reply-To: <22a78829-b726-4949-84a6-20c0ad12cbf3@i6g2000yqj.googlegroups.com> References: <20fa91a0-d406-46f3-aa34-e22881238f4a@i6g2000yqj.googlegroups.com> <9BD579F5-1879-4EAB-B841-D8B69DE2C31A@experthuman.com> <8dcd4945-ef20-4512-8045-e6a1a146e70c@26g2000yqk.googlegroups.com> <62ad3e390907152347j788a8f69q67c0f1940c929064@mail.gmail.com> <22a78829-b726-4949-84a6-20c0ad12cbf3@i6g2000yqj.googlegroups.com> Message-ID: <1A8ECDFE-93FB-47BC-916B-4B45B1066DD4@mattwynne.net> On 16 Jul 2009, at 17:28, internetchris wrote: > Very nice.... the ability to see your workflow helps me a ton. I guess > I needed to see what other developers did. I have a project that I > started, but then quit until I nailed down the testing. I will have to > catch up on the code I have already written, but I'm grasping the > cucumber and rspec correlation now. On anything new I will start with > Cucumber and then to rspec to iron out the actual code. Have you seen these: http://railscasts.com/episodes/155-beginning-with-cucumber http://railscasts.com/episodes/159-more-on-cucumber They'll give you a good feel for how to get started with your cukes. > Thanks! > > Chris > > On Jul 16, 12:47 am, Andrew Timberlake > wrote: >> On Thu, Jul 16, 2009 at 7:46 AM, >> >> internetchris wrote: >>> Tom, >> >>> I appreciate the reply... >> >>> So would I be correct in saying that I should develop all of my spec >>> tests first, and then finish it up by running some cucumber tests? >> >>> Thanks! >> >>> Chris >> >> Chris >> >> If you follow strict Behaviour Driven Development, then the behaviour >> should be defined before the code. Therefore, the Cucumber scenarios >> should come first. >> >> My workflow is like this: >> Write Cucumber scenario - It will fail because the path isn't defined >> Create route entry >> Run Cucumber - It now fails because the controller doesn't exist >> Create controller spec - It will fail because there's no controller >> Create controller >> Spec now fails because required model doesn't exist >> Write model spec - It will fail because model doesn't exist >> Create model >> Flesh out model until model spec passes >> Flesh out controller until controller spec passes >> Continue until Cucumber scenario passes >> Repeat until project is complete :-) >> >> I run autospec so most of the test re-runs are done automatically and >> therefore the process continues a lot more smoothly than it sounds. >> You'll notice that their are no view specs. I don't use view specs >> 95% >> of the time because Cucumber covers this (previously my view specs >> where just an assertion that the view was there). >> When theirs something very specific I need to ensure is in the >> view, I >> might add a view spec. >> >> Andrew Timberlakehttp://ramblingsonrails.com >> >> http://MyMvelope.com- The SIMPLE way to manage your savings >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users cheers, Matt +447974 430184 matt at mattwynne.net http://mattwynne.net From dchelimsky at gmail.com Thu Jul 16 22:25:40 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 16 Jul 2009 21:25:40 -0500 Subject: [rspec-users] [ANN] rspec 1.2.8 Released Message-ID: <57c63afe0907161925t2dbdb65fqbea0159ff8a4fae6@mail.gmail.com> rspec version 1.2.8 has been released! * * * * * Behaviour Driven Development for Ruby. Changes: ### Version 1.2.8 / 2008-07-16 * enhancements * better messages for should[_not] be_nil (Chad Humphries and Rob Sanheim) * should and should_not accept optional custom message (suggestion from Rob Sanheim) * result.should be_nil, "expected result to be nil" * added 'spec/stubs/cucumber' to plug RSpec's stubbing framework into Cucumber scenarios. * added unstub method to remove a stub mid-example (Scott Taylor). Closes #853. * add more readable diff when expected and actual are hashes (Ryan Bigg). Closes #848. * bug fixes * fixed --line option for ruby 1.9.1 * fix stub_chain conflict between two chains starting with the same message (Mike Rohland). Closes #846. From ben at benmabey.com Fri Jul 17 00:39:57 2009 From: ben at benmabey.com (Ben Mabey) Date: Thu, 16 Jul 2009 22:39:57 -0600 Subject: [rspec-users] [ANN] rspec 1.2.8 Released In-Reply-To: <57c63afe0907161925t2dbdb65fqbea0159ff8a4fae6@mail.gmail.com> References: <57c63afe0907161925t2dbdb65fqbea0159ff8a4fae6@mail.gmail.com> Message-ID: <4A60009D.7030907@benmabey.com> David Chelimsky wrote: > rspec version 1.2.8 has been released! > > * > * > * > * > * > > Behaviour Driven Development for Ruby. > > Changes: > > ### Version 1.2.8 / 2008-07-16 > > * enhancements > * better messages for should[_not] be_nil (Chad Humphries and Rob Sanheim) > * should and should_not accept optional custom message > (suggestion from Rob Sanheim) > Ahh, very nice. I've been wanting something like this for a while. For those interested in how to use this feature check out the examples in this cucumber feature for it: http://github.com/dchelimsky/rspec/blob/1420b6f02f5fe2ba97286d895b8215617f80d4a6/features/matchers/define_matcher_with_user_customizable_message.feature Thanks for the release. -Ben > * result.should be_nil, "expected result to be nil" > * added 'spec/stubs/cucumber' to plug RSpec's stubbing framework into > Cucumber scenarios. > * added unstub method to remove a stub mid-example (Scott Taylor). Closes > #853. > * add more readable diff when expected and actual are hashes (Ryan Bigg). > Closes #848. > > * bug fixes > * fixed --line option for ruby 1.9.1 > * fix stub_chain conflict between two chains starting with the same message > (Mike Rohland). Closes #846. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Fri Jul 17 07:10:14 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 17 Jul 2009 06:10:14 -0500 Subject: [rspec-users] [ANN] rspec 1.2.8 Released In-Reply-To: <4A60009D.7030907@benmabey.com> References: <57c63afe0907161925t2dbdb65fqbea0159ff8a4fae6@mail.gmail.com> <4A60009D.7030907@benmabey.com> Message-ID: <57c63afe0907170410n2ceba62bi20e9de868e7ad697@mail.gmail.com> On Thu, Jul 16, 2009 at 11:39 PM, Ben Mabey wrote: > David Chelimsky wrote: >> >> rspec version 1.2.8 has been released! >> >> * >> * >> * >> * >> * >> >> Behaviour Driven Development for Ruby. >> >> Changes: >> >> ### Version 1.2.8 / 2008-07-16 >> >> * enhancements >> ?* better messages for should[_not] be_nil (Chad Humphries and Rob >> Sanheim) >> ?* should and should_not accept optional custom message >> ? ?(suggestion from Rob Sanheim) >> > > Ahh, very nice. ?I've been wanting something like this for a while. ?For > those interested in how to use this feature check out the examples in this > cucumber feature for it: > > http://github.com/dchelimsky/rspec/blob/1420b6f02f5fe2ba97286d895b8215617f80d4a6/features/matchers/define_matcher_with_user_customizable_message.feature Thanks for pointing that out, Ben, but that was an earlier commit and the API changed (before the release, of course). Here's the current cucumber feature for this that shipped w/ the release: http://github.com/dchelimsky/rspec/blob/aa1d064b258a20e4655efe527891acc7f04ec6b2/features/expectations/customized_message.feature Cheers, David > > Thanks for the release. > > -Ben > >> ? ?* result.should be_nil, "expected result to be nil" >> ?* added 'spec/stubs/cucumber' to plug RSpec's stubbing framework into >> ? ?Cucumber scenarios. >> ?* added unstub method to remove a stub mid-example (Scott Taylor). Closes >> ? ?#853. >> ?* add more readable diff when expected and actual are hashes (Ryan Bigg). >> ? ?Closes #848. >> >> * bug fixes >> ?* fixed --line option for ruby 1.9.1 >> ?* fix stub_chain conflict between two chains starting with the same >> message >> ? ?(Mike Rohland). Closes #846. >> _______________________________________________ >> 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 leogodin217 at gmail.com Fri Jul 17 13:08:03 2009 From: leogodin217 at gmail.com (Leo Godin) Date: Fri, 17 Jul 2009 10:08:03 -0700 (PDT) Subject: [rspec-users] testing controller instance variables In-Reply-To: <8d18f1f80907151505q37ba0d13p3ead8fbd3df365ba@mail.gmail.com> References: <8d18f1f80907151505q37ba0d13p3ead8fbd3df365ba@mail.gmail.com> Message-ID: <921c0de7-d960-4dca-b536-7b0b40aa0119@b15g2000yqd.googlegroups.com> Thanks, That works at least to the point where it is testing the variable. However, it is calling it NIL. How do I tell the test to make it be 3? In the controller I have: @num_found = @purchase_requests.size This is what I tried in the spec: it "should count the number of purchase requests found" do @purchase_requests.stub!(:size).and_return 3 get :index assigns(:num_requests).should == 3 end This is the error: 'PurchaseRequestsController Get index should count the number of purchase requests found' FAILED expected: 3, got: nil (using ==) ./spec/controllers/purchase On Jul 15, 3:05?pm, Adam Anderson wrote: > you need to use the assigns method like so > > ?it "should count the number of purchase requests found" do > ? ?get :index > ? ?assigns(:num_found).should == 3 > ?end > > On Wed, Jul 15, 2009 at 2:29 PM, Leo wrote: > > HI All, > > I have a controller with the following in it: > > > ?def index > > ? ?@purchase_requests = PurchaseRequest.find(:all) > > ? ?@num_found = @purchase_requests.size > > ?end > > > I want to test that @num_found is getting the number of purchase > > requests. > > > This is what I've tried: > > > ?it "should count the number of purchase requests found" do > > ? ?@purchase_requests.stub!(:size).and_return 3 > > ? ?@num_found.should_equal(3) > > ? ?get :index > > ?end > > > However, I Get This Error: > > > NoMethodError in 'PurchaseRequestsController Get index should count > > the number of purchase requests found' > > You have a nil object when you didn't expect it! > > The error occurred while evaluating nil.should_equal > > ./spec/controllers/purchase_requests_controller_spec.rb:21: > > > It seems that rspec wants me to stub the instance variable. ?If I do > > that then I don't think I'd actually be testing anything because I'd > > create the stub in the test and verify that the stub I created is > > there. ?I want to make sure that the test will fail if I delete the > > line in the actual controller or change the name. ?Can someone show me > > how to do this? > > thanks in advance. > > Leo > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From rick.denatale at gmail.com Fri Jul 17 15:25:52 2009 From: rick.denatale at gmail.com (Rick DeNatale) Date: Fri, 17 Jul 2009 15:25:52 -0400 Subject: [rspec-users] testing controller instance variables In-Reply-To: <921c0de7-d960-4dca-b536-7b0b40aa0119@b15g2000yqd.googlegroups.com> References: <8d18f1f80907151505q37ba0d13p3ead8fbd3df365ba@mail.gmail.com> <921c0de7-d960-4dca-b536-7b0b40aa0119@b15g2000yqd.googlegroups.com> Message-ID: On Fri, Jul 17, 2009 at 1:08 PM, Leo Godin wrote: > Thanks, > That works at least to the point where it is testing the variable. > However, it is calling it NIL. ?How do I tell the test to make it be > 3? > > In the controller I have: > ?@num_found = @purchase_requests.size > > This is what I tried in the spec: > > ?it "should count the number of purchase requests found" do > ? ?@purchase_requests.stub!(:size).and_return 3 > ? ?get :index > ? ?assigns(:num_requests).should == 3 > ?end > > This is the error: > > 'PurchaseRequestsController Get index should count the number of > purchase requests found' FAILED > expected: 3, > ? ? got: nil (using ==) > ./spec/controllers/purchase Well first off the instance variable that your controller method is setting is @num_found, not @num_requests. So that't the first problem. Then the @purchase_requests IV will refer to a different object when you stub it than after it gets set in your method. So @num_requests is going to be set to the size* of the association returned by the expression @purchase_requests = PurchaseRequest.find(:all) >From your initial post. One tack to take is to stub the PurchaseRequest class itself: it "should count the number of purchase requests found" do purchase = mock("purchase_requests", :size => 3) PurchaseRequest.stub!(:find).and_return(3) get :index assigns(:num_found).should == 3 end * I prefer to use length rather than size in Ruby because size sometimes means the number of elements, and sometimes something else like the number of bytes, depending in the receiver object. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale From celoserpa at gmail.com Mon Jul 20 18:30:03 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Mon, 20 Jul 2009 17:30:03 -0500 Subject: [rspec-users] Cucumber step or model spec? Message-ID: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> Hello list, I have an example/spec that relates to the Product#index view and says "it should display only the active products". The thing is the logic to filter the active products should be in the model, and not on the view, since the view should be "dumb". I just realized that, and what I think is, even though this statement has business value, it is not lower level enough to be a view spec, nor a model or controller spec -- it should be a cucumber step, if any. Could someone enlighten-me, here? :) Thanks in advance, Marcelo. From dchelimsky at gmail.com Mon Jul 20 18:58:56 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 20 Jul 2009 17:58:56 -0500 Subject: [rspec-users] Cucumber step or model spec? In-Reply-To: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> References: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> Message-ID: <57c63afe0907201558x3f25612fg982f5cf4b65bd48@mail.gmail.com> On Mon, Jul 20, 2009 at 5:30 PM, Marcelo de Moraes Serpa wrote: > Hello list, > > ?I have an example/spec that relates to the Product#index view and > says "it should display only the active products". The thing is the > logic to filter the active products should be in the model, and not on > the view, since the view should be "dumb". I just realized that, and > what I think is, even though this statement has business value, it is > not lower level enough to be a view spec, nor a model or controller > spec -- it should be a cucumber step, if any. > > Could someone enlighten-me, here? :) Hi Marcelo, This is a point of confusion for many, so you're not alone. I'll offer my take, though I know there are others who see it differently. I use Cucumber for specifying application behaviour and RSpec for specifying lower level component behaviour. In the scenario you describe: * the application's job is to show only the active products * the view's job is to display any products it is given by the controller * the controller's job is to ask the model for the active products * the model's job is to supply only the active products So for me this is not an either/or question. Each underlying component has a responsibility, and the result of the three components doing the right thing adds up to the application behaving correctly. Now, it happens that the outcomes we expect from the application are the same outcomes we expect from the view. Many see this as duplication, and if you agree with them, then you still may want a cucumber scenario + a controller spec + a model spec. For me, even though they appear to be overlapping, they are not. The outcomes are the same, but the inputs are different. The scenario says the application has to show x given some inputs. The view spec says the view has to show x given some data supplied by the controller. Different behaviour. Now the last question I'd ask myself is whether the duplication in the expected outcomes in the scenario and the view spec buys me anything, and that really depends on how complex the view is, and whether I anticipate doing much refactoring with it (moving partials around, etc). One approach would be to err on the side of less overhead, but then when I do go to refactor, make sure there are relevant view specs in place before I do. HTH, David > > Thanks in advance, > > Marcelo. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From lee.hambley at gmail.com Mon Jul 20 19:01:30 2009 From: lee.hambley at gmail.com (Lee Hambley) Date: Tue, 21 Jul 2009 00:01:30 +0100 Subject: [rspec-users] Cucumber step or model spec? In-Reply-To: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> References: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> Message-ID: Marcelo, My take on this is that if your business user wants to make sure that there are only active products displayed, then it should be in a story. There may be a question about speccing the implementation; however if you are using a named scope - you might argue that you are testing a library (and thus, you shouldn't bother, as the library should test itself.) I would consider a simple spec for named scope to prove that it works. With the named scope tested you can mention it incidentally in your story and not get too hung up on the exact details. I've just noticed I've been writing this draft for so long, David C has also answered, I am as interested as you to read that, so HTH. -- Lee Hambley Twitter: @leehambley Blog: http://lee.hambley.name/ Working with Rails: http://is.gd/1s5W1 2009/7/20 Marcelo de Moraes Serpa > Hello list, > > I have an example/spec that relates to the Product#index view and > says "it should display only the active products". The thing is the > logic to filter the active products should be in the model, and not on > the view, since the view should be "dumb". I just realized that, and > what I think is, even though this statement has business value, it is > not lower level enough to be a view spec, nor a model or controller > spec -- it should be a cucumber step, if any. > > Could someone enlighten-me, here? :) > > Thanks in advance, > > Marcelo. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Mon Jul 20 19:04:16 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 20 Jul 2009 18:04:16 -0500 Subject: [rspec-users] Cucumber step or model spec? In-Reply-To: <57c63afe0907201558x3f25612fg982f5cf4b65bd48@mail.gmail.com> References: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> <57c63afe0907201558x3f25612fg982f5cf4b65bd48@mail.gmail.com> Message-ID: <57c63afe0907201604te4d6f16m9ba220a82962166e@mail.gmail.com> On Mon, Jul 20, 2009 at 5:58 PM, David Chelimsky wrote: > On Mon, Jul 20, 2009 at 5:30 PM, Marcelo de Moraes > Serpa wrote: >> Hello list, >> >> ?I have an example/spec that relates to the Product#index view and >> says "it should display only the active products". The thing is the >> logic to filter the active products should be in the model, and not on >> the view, since the view should be "dumb". I just realized that, and >> what I think is, even though this statement has business value, it is >> not lower level enough to be a view spec, nor a model or controller >> spec -- it should be a cucumber step, if any. >> >> Could someone enlighten-me, here? :) > > Hi Marcelo, > > This is a point of confusion for many, so you're not alone. I'll offer > my take, though I know there are others who see it differently. > > I use Cucumber for specifying application behaviour and RSpec for > specifying lower level component behaviour. In the scenario you > describe: > * the application's job is to show only the active products > * the view's job is to display any products it is given by the controller > * the controller's job is to ask the model for the active products > * the model's job is to supply only the active products > > So for me this is not an either/or question. Each underlying component > has a responsibility, and the result of the three components doing the > right thing adds up to the application behaving correctly. > > Now, it happens that the outcomes we expect from the application are > the same outcomes we expect from the view. Many see this as > duplication, and if you agree with them, then you still may want a > cucumber scenario + a controller spec + a model spec. > > For me, even though they appear to be overlapping, they are not. The > outcomes are the same, but the inputs are different. The scenario says > the application has to show x given some inputs. The view spec says > the view has to show x given some data supplied by the controller. > Different behaviour. > > Now the last question I'd ask myself is whether the duplication in the > expected outcomes in the scenario and the view spec buys me anything, > and that really depends on how complex the view is, and whether I > anticipate doing much refactoring with it (moving partials around, > etc). One approach would be to err on the side of less overhead, but > then when I do go to refactor, make sure there are relevant view specs > in place before I do. Afterthought (or two): You asked about cucumber scenario vs model spec and I responded from a wider viewpoint. I think the same thinking applies to the scenario and the model, though in that case, the expected outcomes are actually different (application renders stuff on a page, model returns the right collection). This is actually a perfect example of when it's good to have view specs because the view template is probably the same view template whether the app is displaying active products, inactive products, or products that shipped last Tuesday. If the view fails to do its job, three scenarios will fail. Perhaps they'll fail in such a way that you can quickly isolate the point of failure. Or, perhaps you'll not be sure whether the problem lies in the view, the controller, or the model. In this case, being able to see passing specs for the view would help point you to the other components. Food for thought. Cheers, David > > HTH, > David > > > > > >> >> Thanks in advance, >> >> Marcelo. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > From chris at silhouettesolutions.net Mon Jul 20 23:05:15 2009 From: chris at silhouettesolutions.net (Chris Sund) Date: Mon, 20 Jul 2009 20:05:15 -0700 (PDT) Subject: [rspec-users] My very fist spec file (not sure what's wrong..) Message-ID: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> Hey Everyone, I am finally to the point in the rspec book where it describes how to implement controller specs, so I thought what the heck, I'll give it a try. I have an app I have been working on (Inside out) that I needed to get test specs written for before I continue on with the app. Now that I have read enough of the book I realize I was doing things a little backwards in the "rspec" sense of BDD, but I thought I would try to apply what I have learned so far to my existing code. Here's the feedback I'm getting when I run my spec file.... NoMethodError in 'AccountsController POST create should build a new account' You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.save /Users/Chris/Rails/obilling/app/controllers/accounts_controller.rb: 24:in `create' ./spec/controllers/accounts_controller_spec.rb:7: script/spec:10: Here's my spec file... require File.expand_path(File.dirname(__FILE__) + '/../spec_helper' ) describe AccountsController, "POST create" do it "should build a new account" do Account.should_receive(:new).with("address" => "1201 washington street") post :create, :account => {"address" => "1201 washington street"} end Here's the create method in my accounts_controller... def create @account = Account.new(params[:account]) if @account.save flash[:notice] = 'Account Record Saved' redirect_to(:action=>'edit', :id => @account) else flash[:warning] = 'Account Record Did Not Save!' render :action => 'new' end end Keep in mind I'm a noob to everything (except programming). I have been reading my brains out trying to get a better grasp on the ruby syntax and how to apply this to rspec and rails. If someone has a moment to explain this error. I understand that the @account instance variable is "nil" in the error, but I don't understand why. I'm sure it has something to do with -- post :create, :account => {"address" => "1201 washington street"}. It must be a syntax problem, or the way I'm defining :account. Thanks! Chris From jonathan at parkerhill.com Mon Jul 20 22:54:21 2009 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Mon, 20 Jul 2009 22:54:21 -0400 Subject: [rspec-users] Cucumber step or model spec? In-Reply-To: <57c63afe0907201558x3f25612fg982f5cf4b65bd48@mail.gmail.com> References: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> <57c63afe0907201558x3f25612fg982f5cf4b65bd48@mail.gmail.com> Message-ID: On Jul 20, 2009, at 6:58 PM, David Chelimsky wrote: > So for me this is not an either/or question. Each underlying component > has a responsibility, and the result of the three components doing the > right thing adds up to the application behaving correctly. I'd also add that, for me, the cucumber story tends to exercise the more "typical" cases whereas my specs are more thorough in covering edge cases and exceptions. linoj From ben at benmabey.com Mon Jul 20 23:21:28 2009 From: ben at benmabey.com (Ben Mabey) Date: Mon, 20 Jul 2009 21:21:28 -0600 Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> Message-ID: <4A653438.2040005@benmabey.com> Chris Sund wrote: > Hey Everyone, > > I am finally to the point in the rspec book where it describes how to > implement controller specs, so I thought what the heck, I'll give it a > try. I have an app I have been working on (Inside out) that I needed > to get test specs written for before I continue on with the app. Now > that I have read enough of the book I realize I was doing things a > little backwards in the "rspec" sense of BDD, but I thought I would > try to apply what I have learned so far to my existing code. > > Here's the feedback I'm getting when I run my spec file.... > > NoMethodError in 'AccountsController POST create should build a new > account' > You have a nil object when you didn't expect it! > You might have expected an instance of ActiveRecord::Base. > The error occurred while evaluating nil.save > /Users/Chris/Rails/obilling/app/controllers/accounts_controller.rb: > 24:in `create' > ./spec/controllers/accounts_controller_spec.rb:7: > script/spec:10: > > > Here's my spec file... > > require File.expand_path(File.dirname(__FILE__) + '/../spec_helper' ) > > describe AccountsController, "POST create" do > > it "should build a new account" do > Account.should_receive(:new).with("address" => "1201 washington > street") > This is your problem.. you need to return something for 'save' to be called on. i.e. Account.should_receive(:new).with("address" => "1201 washington street").and_return(mock_model(Accrount)) Notice the "and_return" at the end? That way a mock_model of Account will be returned from the new call so that the controller can set it to @account and save it. To see some examples of RSpec controller specs you can use a generator to general rspec scaffolding... like so: ./script/generate rspec_scaffold Account HTH, Ben From sfeley at gmail.com Mon Jul 20 23:45:54 2009 From: sfeley at gmail.com (Stephen Eley) Date: Mon, 20 Jul 2009 23:45:54 -0400 Subject: [rspec-users] how to spec a recursive method that creates db records? In-Reply-To: <83e853830907201733m3619394ck2f75ef1918c4fcc4@mail.gmail.com> References: <83e853830907201733m3619394ck2f75ef1918c4fcc4@mail.gmail.com> Message-ID: <1fb4df0907202045t632dafa4l7b362a37a8d79f4e@mail.gmail.com> On Mon, Jul 20, 2009 at 8:33 PM, Barun Singh wrote: > > I want to find a way to write a spec for this method that does both of these > things: > (1)? stub out the do_something method (that method has its own specs) > (2)? not stub out the logic in the else statement, (there is some complex > logic in there involving sql queries on multiple tables and i explicitly > want to make it touch the database so that I can examine the state of the > database after the method is run in my spec.? a lot of complex stubs would > be required to do this while also having the spec that is actually useful) Okay, first off -- I think part of your problem is that your focus is too short. It sounds like you're asking how to write different specs for various lines of the _internals_ of this method. That's too fine-grained. Spec the method from the _outside:_ write specs that say "If I give the method these inputs, this should happen." Then test that the end result of the method's execution was what you expect. I'm betting that probably sounds really difficult, given the "complex logic" you're describing. And that would be my second, more important tip: if your method is so complicated that you can't spec it from the outside, you should take that as a sign. Spaghetti code is usually very hard to spec. Reconsider your problem. Try breaking it down into smaller, more easily specified pieces, and then recompose them into the logic that you need. I don't know your business rules; it looks to me like the basic task here is "Keep twiddling with the database until I get what I'm looking for, as many times as that takes." I don't know why that's necessary, but if it is, recursion doesn't seem like the most elegant answer to me. Recursive calls are usually made in small functions that don't have side effects. This is the opposite. What would I do instead? Again, without knowing your specific business, I'd probably break it into pieces, and then turn the requirement inside out and do it with a loop: def self.find_something(inputs) end def self.add_something(inputs) end And then, wherever you *would* have called mymethod(inputs), you can instead write this, which will loop zero or more times: add_something_to_db(inputs) until x = find_something(inputs) x.do_something ...Whether or not you still want to wrap that in a method probably depends on whether you need to call it more than once. But I'll bet you that the "add_something" and "find_something" methods are both easier to spec from the outside. If they're still too complex, then break them down too. Oh, and final trivia note: > any thoughts on how to accomplish this?? two approaches that would make > sense but don't seem to be possible as far as i know are: > (A)? stub out the do_something method for any instance of the class (i can't > stub it out for a particular record because the records don't exist at the > time of calling the method and I'm not stubbing the creation process) > (B) stub out "mymethod" only for the second time it is called (obviously i > can't stub the method out entirely since that's the method i'm testing) You can do both of these with the Mocha mocking library, if you really want to. It puts an "any_instance" method on Class that you can use to mock or stub; and you can specify ordered chains of results to return. But again, you shouldn't need to if your methods are atomic enough. If 'do_something' properly belongs outside the scope of what you're testing, then maybe that's a sign that you shouldn't put it in the same block of code. And if you find a need to declare that your method should behave entirely differently on a second invocation, maybe it shouldn't be one method. Simplify relentlessly. Once I figured out how to make my code simpler, I found that I wasn't mocking nearly as much as I used to. -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From sfeley at gmail.com Mon Jul 20 23:59:17 2009 From: sfeley at gmail.com (Stephen Eley) Date: Mon, 20 Jul 2009 23:59:17 -0400 Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> Message-ID: <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> On Mon, Jul 20, 2009 at 11:05 PM, Chris Sund wrote: > > I am finally to the point in the rspec book where it describes how to > implement controller specs, so I thought what the heck, I'll give it a > try. Heh. Points to you for ambition! You might not have realized this (because the RSpec book makes it sound like it should *all* be easy) but starting with a Rails controller spec for your very first spec is a bit like saying "I was thinking of getting scuba certified... Ooooh, CAVE DIVING! Let's start with that first!"* On the upside, if you start there and really get to *understand* what's going on, you should be relieved when most of the rest of it is pretty smooth sailing. There are very few common Ruby idioms that have such tight coupling as Rails controllers, so very few tasks are so hard to spec in isolation. You'll almost never have to mock anything else as ferociously. Model specs in particular will feel like sunshine and puppies. So go you! -- Have Fun, *(Granted, I don't think anyone has ever kicked up silt while writing a controller spec, lost hold of their guide line, gotten hopelessly lost, and died many hours later in the cold and dark. Yet. That only happens to J2EE programmers.) Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From sfeley at gmail.com Tue Jul 21 02:31:58 2009 From: sfeley at gmail.com (Stephen Eley) Date: Tue, 21 Jul 2009 02:31:58 -0400 Subject: [rspec-users] how to spec a recursive method that creates db records? In-Reply-To: <83e853830907202305p476662a4we4ba558bfecbdb90@mail.gmail.com> References: <83e853830907201733m3619394ck2f75ef1918c4fcc4@mail.gmail.com> <1fb4df0907202045t632dafa4l7b362a37a8d79f4e@mail.gmail.com> <83e853830907202305p476662a4we4ba558bfecbdb90@mail.gmail.com> Message-ID: <1fb4df0907202331r69d3edebt2d1e02bda70f4817@mail.gmail.com> On Tue, Jul 21, 2009 at 2:05 AM, Barun Singh wrote: > This isn't a question of refactoring; I can > easily refactor the method as you describe but this doesn't resolve the > issue (indeed, it just leads to an increased number of public methods in the > model with no real benefit). Why would they have to be public? > I can easily spec the find_something and add_something methods so I know > that they work correctly.? My question is, how do I write a spec for > find_or_add_something in a way where I don't have to re-test the behavior of > the individual find_something and add_something methods a second time, and > can just test the overall logic of that method? My answer for that remains the same: spec it from the outside. Treat it as a black box. Set up an initial state, then write specs that express the expectation that inputs A, B, and C should yield output D. (Where D might be a database record, or a returned value, or both, or whatever.) The number of specs you write in this manner will depend on the number of interesting ways that your method could go right or wrong. You may need to provide multiple initial states as well, but that's easy in RSpec with nested 'describe' or 'context' blocks. That the method calls 'add_something' and 'find_something' on the inside is immaterial. You don't have to isolate everything from everything else. If we stubbed every method call inside every method we tested, we'd soon get to the point where we were stubbing '+' or 'gsub' or 'print.' Your specs shouldn't have to care how 'find_and_add_something' works. What you are expressing is that it DOES work. Test the inputs and outputs, and let it call whatever it wants to get from one to the other. -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From dchelimsky at gmail.com Tue Jul 21 05:49:52 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 21 Jul 2009 04:49:52 -0500 Subject: [rspec-users] how to spec a recursive method that creates db records? In-Reply-To: <83e853830907202305p476662a4we4ba558bfecbdb90@mail.gmail.com> References: <83e853830907201733m3619394ck2f75ef1918c4fcc4@mail.gmail.com> <1fb4df0907202045t632dafa4l7b362a37a8d79f4e@mail.gmail.com> <83e853830907202305p476662a4we4ba558bfecbdb90@mail.gmail.com> Message-ID: <57c63afe0907210249j4f0db9adocec7b25cd9a0fa6c@mail.gmail.com> Hi Barun, On Tue, Jul 21, 2009 at 1:05 AM, Barun Singh wrote: > I certainly appreciate the thoughtful and lengthy reply, but I think it > misses the main part of my problem -- perhaps my original explanation of my > issue was lacking in clarity.? This isn't a question of refactoring; I can > easily refactor the method as you describe but this doesn't resolve the > issue (indeed, it just leads to an increased number of public methods in the > model with no real benefit). You want to know your code works, right? Testability _is_ a real benefit. Obviously everything needs to be balanced, and if you can do things in a pure API, non-invasive way, you're going to end up with a more flexible result. But sometimes we have to make tradeoffs. > So, to more clearly illustrate my question, > forget about my original method and instead let's say I have three methods > as follows: > > def self.find_something(inputs) > ? .. do stuff here > end > > def self.add_something(inputs) > ? .. do stuff here > end > > def self.find_or_add_something(inputs) > ? x = self.find_something(inputs) > ? if !x > ??? self.add_something(inputs) > ??? x = self.find_something(inputs) > ? end > ? x > end > > I can easily spec the find_something and add_something methods so I know > that they work correctly.? My question is, how do I write a spec for > find_or_add_something in a way where I don't have to re-test the behavior of > the individual find_something and add_something methods a second time, and > can just test the overall logic of that method? This is _very_ invasive and brittle, and I'd likely not approach it this way myself, but it works: http://gist.github.com/151226 HTH, David > > It's helpful to know that Mocha is available to assist with this, but I'd > rather not switch to an entirely different framework for mocks & stubs just > because of this one method... > > > > On Mon, Jul 20, 2009 at 11:45 PM, Stephen Eley wrote: >> >> On Mon, Jul 20, 2009 at 8:33 PM, Barun Singh wrote: >> > >> > I want to find a way to write a spec for this method that does both of >> > these >> > things: >> > (1)? stub out the do_something method (that method has its own specs) >> > (2)? not stub out the logic in the else statement, (there is some >> > complex >> > logic in there involving sql queries on multiple tables and i explicitly >> > want to make it touch the database so that I can examine the state of >> > the >> > database after the method is run in my spec.? a lot of complex stubs >> > would >> > be required to do this while also having the spec that is actually >> > useful) >> >> Okay, first off -- I think part of your problem is that your focus is >> too short. ?It sounds like you're asking how to write different specs >> for various lines of the _internals_ of this method. ?That's too >> fine-grained. ?Spec the method from the _outside:_ write specs that >> say "If I give the method these inputs, this should happen." ?Then >> test that the end result of the method's execution was what you >> expect. >> >> I'm betting that probably sounds really difficult, given the "complex >> logic" you're describing. ?And that would be my second, more important >> tip: if your method is so complicated that you can't spec it from the >> outside, you should take that as a sign. ?Spaghetti code is usually >> very hard to spec. >> >> Reconsider your problem. ?Try breaking it down into smaller, more >> easily specified pieces, and then recompose them into the logic that >> you need. ?I don't know your business rules; it looks to me like the >> basic task here is "Keep twiddling with the database until I get what >> I'm looking for, as many times as that takes." ?I don't know why >> that's necessary, but if it is, recursion doesn't seem like the most >> elegant answer to me. ?Recursive calls are usually made in small >> functions that don't have side effects. ?This is the opposite. >> >> What would I do instead? ?Again, without knowing your specific >> business, I'd probably break it into pieces, and then turn the >> requirement inside out and do it with a loop: >> >> def self.find_something(inputs) >> ? >> end >> >> def self.add_something(inputs) >> ? >> end >> >> And then, wherever you *would* have called mymethod(inputs), you can >> instead write this, which will loop zero or more times: >> >> add_something_to_db(inputs) until x = find_something(inputs) >> x.do_something >> >> ...Whether or not you still want to wrap that in a method probably >> depends on whether you need to call it more than once. ?But I'll bet >> you that the "add_something" and "find_something" methods are both >> easier to spec from the outside. ?If they're still too complex, then >> break them down too. >> >> >> Oh, and final trivia note: >> >> > any thoughts on how to accomplish this?? two approaches that would make >> > sense but don't seem to be possible as far as i know are: >> > (A)? stub out the do_something method for any instance of the class (i >> > can't >> > stub it out for a particular record because the records don't exist at >> > the >> > time of calling the method and I'm not stubbing the creation process) >> > (B) ?stub out "mymethod" only for the second time it is called >> > (obviously i >> > can't stub the method out entirely since that's the method i'm testing) >> >> You can do both of these with the Mocha mocking library, if you really >> want to. ?It puts an "any_instance" method on Class that you can use >> to mock or stub; and you can specify ordered chains of results to >> return. >> >> But again, you shouldn't need to if your methods are atomic enough. >> If 'do_something' properly belongs outside the scope of what you're >> testing, then maybe that's a sign that you shouldn't put it in the >> same block of code. ?And if you find a need to declare that your >> method should behave entirely differently on a second invocation, >> maybe it shouldn't be one method. >> >> Simplify relentlessly. ?Once I figured out how to make my code >> simpler, I found that I wasn't mocking nearly as much as I used to. >> >> >> -- >> Have Fun, >> ? Steve Eley (sfeley at gmail.com) >> ? ESCAPE POD - The Science Fiction Podcast Magazine >> ? http://www.escapepod.org >> _______________________________________________ >> 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 dchelimsky at gmail.com Tue Jul 21 09:24:30 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 21 Jul 2009 08:24:30 -0500 Subject: [rspec-users] how to spec a recursive method that creates db records? In-Reply-To: <83e853830907210615r143c98c3ma3615aa1f3e15eb3@mail.gmail.com> References: <83e853830907201733m3619394ck2f75ef1918c4fcc4@mail.gmail.com> <1fb4df0907202045t632dafa4l7b362a37a8d79f4e@mail.gmail.com> <83e853830907202305p476662a4we4ba558bfecbdb90@mail.gmail.com> <57c63afe0907210249j4f0db9adocec7b25cd9a0fa6c@mail.gmail.com> <83e853830907210615r143c98c3ma3615aa1f3e15eb3@mail.gmail.com> Message-ID: <57c63afe0907210624pdbf33ffm2eaddb1d31cd3cc4@mail.gmail.com> On Tue, Jul 21, 2009 at 8:15 AM, Barun Singh wrote: > I completely agree that testability is a huge benefit, and if changing code > structure makes that easier then I'm all for it.? My statement was only > meant to imply that I didn't find testability to become any easier by > refactoring this particular method, and absent that I didn't see any reason > to split it up.? But if testability is actually improved, I'd be eager to > make changes appropriately. > > I looked over your code in the gist (which, by the way I really appreciate > you taking the time to write out), but I don't see how this tests the method > properly.? The result of the else condition is never actually tested. > Specifically, I thing the tests would all pass, even if > find_or_add_something looked like this: > > ? def self.find_or_add_something(inputs) > ????if x = self.find_something(inputs) > ??????x > ????else > ??????self.add_something(inputs) > ????? "blah" > ??? end > ??end > (if it isn't clear, the difference is that I'm not calling find_something in > the else condition and am instead just returning "blah"). > > This is because the last test "#find_or_add_something when find something > returns?nil returns the result of find_something" doesn't actually specify > that find_something returns nil the first time it is called.? So that last > spec describes what I want to test but what it actually tests is the same > thing as "#find_or_add_something when find something returns a Thing returns > the Thing" [line 39]. > > At least this is what it seems like to me; if I'm missing something > glaringly obvious (possible) please do point it out to me. > > Hopefully with this concrete example of a set of specs that you've provided > my question is a bit clearer. I changed the very last example in the gist so that the first time it receives find_something it returns nil, then the 2nd time it returns the thing. Try again: http://gist.github.com/151226 > > > > On Tue, Jul 21, 2009 at 5:49 AM, David Chelimsky > wrote: >> >> Hi Barun, >> >> On Tue, Jul 21, 2009 at 1:05 AM, Barun Singh wrote: >> > I certainly appreciate the thoughtful and lengthy reply, but I think it >> > misses the main part of my problem -- perhaps my original explanation of >> > my >> > issue was lacking in clarity.? This isn't a question of refactoring; I >> > can >> > easily refactor the method as you describe but this doesn't resolve the >> > issue (indeed, it just leads to an increased number of public methods in >> > the >> > model with no real benefit). >> >> You want to know your code works, right? Testability _is_ a real >> benefit. Obviously everything needs to be balanced, and if you can do >> things in a pure API, non-invasive way, you're going to end up with a >> more flexible result. But sometimes we have to make tradeoffs. >> >> > So, to more clearly illustrate my question, >> > forget about my original method and instead let's say I have three >> > methods >> > as follows: >> > >> > def self.find_something(inputs) >> > ? .. do stuff here >> > end >> > >> > def self.add_something(inputs) >> > ? .. do stuff here >> > end >> > >> > def self.find_or_add_something(inputs) >> > ? x = self.find_something(inputs) >> > ? if !x >> > ??? self.add_something(inputs) >> > ??? x = self.find_something(inputs) >> > ? end >> > ? x >> > end >> > >> > I can easily spec the find_something and add_something methods so I know >> > that they work correctly.? My question is, how do I write a spec for >> > find_or_add_something in a way where I don't have to re-test the >> > behavior of >> > the individual find_something and add_something methods a second time, >> > and >> > can just test the overall logic of that method? >> >> This is _very_ invasive and brittle, and I'd likely not approach it >> this way myself, but it works: >> >> http://gist.github.com/151226 >> >> HTH, >> David >> >> > >> > It's helpful to know that Mocha is available to assist with this, but >> > I'd >> > rather not switch to an entirely different framework for mocks & stubs >> > just >> > because of this one method... >> > >> > >> > >> > On Mon, Jul 20, 2009 at 11:45 PM, Stephen Eley wrote: >> >> >> >> On Mon, Jul 20, 2009 at 8:33 PM, Barun Singh wrote: >> >> > >> >> > I want to find a way to write a spec for this method that does both >> >> > of >> >> > these >> >> > things: >> >> > (1)? stub out the do_something method (that method has its own specs) >> >> > (2)? not stub out the logic in the else statement, (there is some >> >> > complex >> >> > logic in there involving sql queries on multiple tables and i >> >> > explicitly >> >> > want to make it touch the database so that I can examine the state of >> >> > the >> >> > database after the method is run in my spec.? a lot of complex stubs >> >> > would >> >> > be required to do this while also having the spec that is actually >> >> > useful) >> >> >> >> Okay, first off -- I think part of your problem is that your focus is >> >> too short. ?It sounds like you're asking how to write different specs >> >> for various lines of the _internals_ of this method. ?That's too >> >> fine-grained. ?Spec the method from the _outside:_ write specs that >> >> say "If I give the method these inputs, this should happen." ?Then >> >> test that the end result of the method's execution was what you >> >> expect. >> >> >> >> I'm betting that probably sounds really difficult, given the "complex >> >> logic" you're describing. ?And that would be my second, more important >> >> tip: if your method is so complicated that you can't spec it from the >> >> outside, you should take that as a sign. ?Spaghetti code is usually >> >> very hard to spec. >> >> >> >> Reconsider your problem. ?Try breaking it down into smaller, more >> >> easily specified pieces, and then recompose them into the logic that >> >> you need. ?I don't know your business rules; it looks to me like the >> >> basic task here is "Keep twiddling with the database until I get what >> >> I'm looking for, as many times as that takes." ?I don't know why >> >> that's necessary, but if it is, recursion doesn't seem like the most >> >> elegant answer to me. ?Recursive calls are usually made in small >> >> functions that don't have side effects. ?This is the opposite. >> >> >> >> What would I do instead? ?Again, without knowing your specific >> >> business, I'd probably break it into pieces, and then turn the >> >> requirement inside out and do it with a loop: >> >> >> >> def self.find_something(inputs) >> >> ? >> >> end >> >> >> >> def self.add_something(inputs) >> >> ? >> >> end >> >> >> >> And then, wherever you *would* have called mymethod(inputs), you can >> >> instead write this, which will loop zero or more times: >> >> >> >> add_something_to_db(inputs) until x = find_something(inputs) >> >> x.do_something >> >> >> >> ...Whether or not you still want to wrap that in a method probably >> >> depends on whether you need to call it more than once. ?But I'll bet >> >> you that the "add_something" and "find_something" methods are both >> >> easier to spec from the outside. ?If they're still too complex, then >> >> break them down too. >> >> >> >> >> >> Oh, and final trivia note: >> >> >> >> > any thoughts on how to accomplish this?? two approaches that would >> >> > make >> >> > sense but don't seem to be possible as far as i know are: >> >> > (A)? stub out the do_something method for any instance of the class >> >> > (i >> >> > can't >> >> > stub it out for a particular record because the records don't exist >> >> > at >> >> > the >> >> > time of calling the method and I'm not stubbing the creation process) >> >> > (B) ?stub out "mymethod" only for the second time it is called >> >> > (obviously i >> >> > can't stub the method out entirely since that's the method i'm >> >> > testing) >> >> >> >> You can do both of these with the Mocha mocking library, if you really >> >> want to. ?It puts an "any_instance" method on Class that you can use >> >> to mock or stub; and you can specify ordered chains of results to >> >> return. >> >> >> >> But again, you shouldn't need to if your methods are atomic enough. >> >> If 'do_something' properly belongs outside the scope of what you're >> >> testing, then maybe that's a sign that you shouldn't put it in the >> >> same block of code. ?And if you find a need to declare that your >> >> method should behave entirely differently on a second invocation, >> >> maybe it shouldn't be one method. >> >> >> >> Simplify relentlessly. ?Once I figured out how to make my code >> >> simpler, I found that I wasn't mocking nearly as much as I used to. >> >> >> >> >> >> -- >> >> Have Fun, >> >> ? Steve Eley (sfeley at gmail.com) >> >> ? ESCAPE POD - The Science Fiction Podcast Magazine >> >> ? http://www.escapepod.org >> >> _______________________________________________ >> >> 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 > From sfeley at gmail.com Tue Jul 21 10:22:09 2009 From: sfeley at gmail.com (Stephen Eley) Date: Tue, 21 Jul 2009 10:22:09 -0400 Subject: [rspec-users] how to spec a recursive method that creates db records? In-Reply-To: <83e853830907210018o75ffbe7u673cfcdb16744e03@mail.gmail.com> References: <83e853830907201733m3619394ck2f75ef1918c4fcc4@mail.gmail.com> <1fb4df0907202045t632dafa4l7b362a37a8d79f4e@mail.gmail.com> <83e853830907202305p476662a4we4ba558bfecbdb90@mail.gmail.com> <1fb4df0907202331r69d3edebt2d1e02bda70f4817@mail.gmail.com> <83e853830907210018o75ffbe7u673cfcdb16744e03@mail.gmail.com> Message-ID: <1fb4df0907210722o5540ce62xf90810d1ceb9a0d2@mail.gmail.com> On Tue, Jul 21, 2009 at 3:18 AM, Barun Singh wrote: > I get your point, and I already understood it well before my original > email.? We all know that generic advice isn't always applicable in every > instance, however, and this is a case where the number of distinct specs > required to test all input combinations that are of interest is simply too > large to make it worth doing if I don't use stubs at all. That's the kind of problem I like to avoid. My experience is that it can _usually_ be avoided by reexamining something up the chain. Sometimes the method can be turned into a class, or the data model can be restructured, or the business rules that make it overly complex aren't as inflexible as I first thought they were. Not always -- sometimes we really do have to work with what we're given, and what we're given is a mess. That may be your case, but you didn't give enough information for me _not_ to raise "Look at the big picture" as a possibility. If that's really the case, though, then the kind of testing you need to do on this algorithm changes. "A bunch of pieces with complex states need to work together" feels more like an integration test to me than a unit test. And I don't understand how stubbing helps you get better integration test coverage. Stubbing means locking down the state of something. If you stub something internal to your code, you're no longer testing cases for states your stub doesn't return. If those cases matter during 'black box' testing, but you don't hit them when you abstract something out, you've just created a gap. (And if you *do* hit them despite abstracting out, then why do you need the code that got stubbed?) > It's obviously hyperbolic and a bit > silly to suggest that stubbing out a public method from one of my models in > order to simplify a spec would lead to stubbing "every method call inside > every method".? You might as well argue that a person should never stub > anything in any spec. You're right that I was hyperbolic. But I treat it as a red flag if I feel a need to stub code I've already written in the same class or module as what I'm testing. I usually stub for two reasons: 1.) As a design placeholder for stuff I haven't written yet (and I take those stubs out after I've written them); or 2.) As a placeholder for *external* interface points to services, libraries, or functional spheres that I need to couple with but don't want to get highly entangled with. Stubbing improves speed for slow stuff, but it also compels me to keep my interactions simple. (Examples: stubbing out results from a SOAP service, or stubbing out "current_user" on controller specs.) I do a lot more mocking, but usually just to confirm that particular side effects were triggered. (And again: I take having to do it as a cue to at least consider whether I can write the code in a way that minimizes side effects.) I'm not trying to be an ass about this. I think this is a good conversation. You know your problem domain better than I do, and you're clearly not a novice at this stuff. But telling me "Thanks, but you're wrong" when you didn't say a lot about the problem domain makes it hard to be helpful. > My statement of the refactored methods being public was predicated on the > assumption that I would be stubbing them out (I don't stub private methods), > since there's no other useful reason to refactor in this instance (the code > isn't hard to read, I don't need to reuse any of it elsewhere, and if I > don't stub them out in testing the outer method then refactoring hasn't made > any tests any easier either). But a useful reason to refactor *did* come up. Your subject line for this thread was about how to spec a recursive method. You've had several demonstrations of ways to make spec'ing easier by not recursing. Your second block of code, with "add_something" and "find_something," is more readable *and* more testable, whether or not you stub. That's a win, right? -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From chris at silhouettesolutions.net Tue Jul 21 11:25:25 2009 From: chris at silhouettesolutions.net (internetchris) Date: Tue, 21 Jul 2009 08:25:25 -0700 (PDT) Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> Message-ID: <16720437-4bdd-4552-8c22-58bc523be059@c29g2000yqd.googlegroups.com> Ben that worked perfectly I appreciate the help. Stephen, I appreciate the encouragement, it feels daunting to be learning all of this at once, but each day I bite off a little more understanding. It's funny you mention scuba diving - "way back when" right out of high school I thought it would be fun to go to school for underwater construction/welding - so I did. I suppose if I was able to tackle that, I will eventually get this. I'm thankful I decided to switch my degree path to computer science after that however. It was an interesting part of my life to say the least. Maybe it's a personality quirk of mine :-) Thanks! Chris On Jul 20, 9:59?pm, Stephen Eley wrote: > On Mon, Jul 20, 2009 at 11:05 PM, Chris > > Sund wrote: > > > I am finally to the point in the rspec book where it describes how to > > implement controller specs, so I thought what the heck, I'll give it a > > try. > > Heh. ?Points to you for ambition! ?You might not have realized this > (because the RSpec book makes it sound like it should *all* be easy) > but starting with a Rails controller spec for your very first spec is > a bit like saying "I was thinking of getting scuba certified... > Ooooh, CAVE DIVING! ?Let's start with that first!"* > > On the upside, if you start there and really get to *understand* > what's going on, you should be relieved when most of the rest of it is > pretty smooth sailing. ?There are very few common Ruby idioms that > have such tight coupling as Rails controllers, so very few tasks are > so hard to spec in isolation. ?You'll almost never have to mock > anything else as ferociously. ?Model specs in particular will feel > like sunshine and puppies. > > So go you! > > -- > Have Fun, ?*(Granted, I don't think anyone has ever kicked up silt > while writing a controller spec, lost hold of their guide line, gotten > hopelessly lost, and died many hours later in the cold and dark. ?Yet. > ?That only happens to J2EE programmers.) > ? ?Steve Eley (sfe... at gmail.com) > ? ?ESCAPE POD - The Science Fiction Podcast Magazine > ? ?http://www.escapepod.org > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From celoserpa at gmail.com Tue Jul 21 11:56:37 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Tue, 21 Jul 2009 10:56:37 -0500 Subject: [rspec-users] Cucumber step or model spec? In-Reply-To: References: <1e5bcefd0907201530p532cb5b9ra447bf7a57a367ca@mail.gmail.com> <57c63afe0907201558x3f25612fg982f5cf4b65bd48@mail.gmail.com> Message-ID: <1e5bcefd0907210856i3695648cw29699f5fd42fe7a2@mail.gmail.com> Thank you all for the replies! >I use Cucumber for specifying application behaviour and RSpec for >specifying lower level component behaviour. In the scenario you >describe: >* the application's job is to show only the active products >* the view's job is to display any products it is given by the controller >* the controller's job is to ask the model for the active products >* the model's job is to supply only the active products This was the most useful piece of information I've seen lately. I think I was still not thinking with this behaviour mindset, and asking this question -- "what is the responsibility/job of the controller in this context" really helped me to find out that the controller's job is to ask the model for the data, and this is exactly what I need to spec -- if he is sending the right message and setting the right variables. By the way, great work on the rspec book, it has been really helping me to master the art of BDD, along with your tireless support here in the mailing list. Thanks a lot for the support! Regards, Marcelo. On Mon, Jul 20, 2009 at 9:54 PM, Jonathan Linowes wrote: > > On Jul 20, 2009, at 6:58 PM, David Chelimsky wrote: > >> So for me this is not an either/or question. Each underlying component >> has a responsibility, and the result of the three components doing the >> right thing adds up to the application behaving correctly. > > I'd also add that, for me, the cucumber story tends to exercise the more > "typical" cases whereas my specs are more thorough in covering edge cases > and exceptions. > > linoj > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Tue Jul 21 14:57:41 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 21 Jul 2009 13:57:41 -0500 Subject: [rspec-users] Rspec Rails 1.2.7.1 In-Reply-To: <5a24fdec0907211151r68c91d88sbd08440382f4be41@mail.gmail.com> References: <5a24fdec0907211151r68c91d88sbd08440382f4be41@mail.gmail.com> Message-ID: <57c63afe0907211157w173f3582k8ad734d7509ca67f@mail.gmail.com> On Tue, Jul 21, 2009 at 1:51 PM, Peer Allan wrote: > Hello all, > Trying to update to rspec 1.2.8, but I keep getting rspec-rails 1.2.7.1, is > that what I should be expecting? There were no updates to rspec-rails, so the current releases are rspec-rails-1.2.7.1 and rspec-1.2.8. Cheers, David > Peer From mpd at particularplace.com Tue Jul 21 14:14:25 2009 From: mpd at particularplace.com (mpd) Date: Tue, 21 Jul 2009 11:14:25 -0700 (PDT) Subject: [rspec-users] [RAILS] - 2.3.3 creating a sea of red Message-ID: hello, I've already asked via Rails channels on this and have received no response, so I'm asking here in hopes someone has run into something similar. I updated to rails 2.3.3 yesterday, and now all of my specs are failing with the following error: Fixture::FormatError in 'PublishedGallery Methods#thumbnail should delegate to its lead asset' a YAML error occurred parsing /Users/mpd/timber/spec/fixtures/ galleries.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html The exact error was: NoMethodError: undefined method `abstract_class?' for Object:Class A full stack trace (and other info like the list of system gems) is available at: http://gist.github.com/151492 The real problem has nothing to do with YAML. I can go green right now by changing RAILS_GEM_VERSION in environment.rb back to 2.3.2. I'm sure this is due to some Rails change, but I can't find any info about it. Only running my specs is broken, both via rake and the spec command. I can use script/console with both test and development environments with no problems, and the app looks to be running fine. It just looks like a load-order problem somewhere, but I'm not sure where to start debugging. thanks From vagostino at gmail.com Tue Jul 21 14:00:44 2009 From: vagostino at gmail.com (Val) Date: Tue, 21 Jul 2009 11:00:44 -0700 (PDT) Subject: [rspec-users] "Cucumber database is not configured" error Message-ID: Hi, Hoping someone can help me out here. Whenever I try to run cucumber features I keep getting an error that "cucumber database is not configured". Stack trace is below. Here's some background info: * I can successfully run all features on a different computer. This error is being generated on my laptop (same code base via git), so I suspect that it's a gem/configuration problem and not a problem with my project code. * Using cucumber 0.3.11, rspec 1.2.7 * Have removed and reinstalled all rspec/cucumber gems and dependencies following wiki instructions multiple times, with no remedy * Can run full rspec suite fine * Have dropped and recreated test database I've googled around and not found anything referencing this error, so hope someone can help me track it down. Thanks! Val cucumber database is not configured (ActiveRecord::AdapterNotSpecified) /usr/local/src/thrivebaby/vendor/rails/activerecord/lib/active_record/ connection_adapters/abstract/connection_specification.rb:62:in `establish_connection' /usr/local/src/thrivebaby/vendor/rails/activerecord/lib/active_record/ connection_adapters/abstract/connection_specification.rb:55:in `establish_connection' /usr/local/src/thrivebaby/config/../vendor/rails/railties/lib/ initializer.rb:417:in `initialize_database' /usr/local/src/thrivebaby/config/../vendor/rails/railties/lib/ initializer.rb:141:in `process' /usr/local/src/thrivebaby/config/../vendor/rails/railties/lib/ initializer.rb:113:in `send' /usr/local/src/thrivebaby/config/../vendor/rails/railties/lib/ initializer.rb:113:in `run' /usr/local/src/thrivebaby/config/environment.rb:10 /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require' /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require' /Library/Ruby/Gems/1.8/gems/polyglot-0.2.6/lib/polyglot.rb:55:in `require' ./features/support/env.rb:3 /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require' /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require' /Library/Ruby/Gems/1.8/gems/polyglot-0.2.6/lib/polyglot.rb:55:in `require' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/../lib/cucumber/cli/ main.rb:95:in `require_files' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/../lib/cucumber/cli/ main.rb:104:in `each_lib' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/../lib/cucumber/cli/ main.rb:102:in `each' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/../lib/cucumber/cli/ main.rb:102:in `each_lib' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/../lib/cucumber/cli/ main.rb:95:in `require_files' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/../lib/cucumber/cli/ main.rb:47:in `execute!' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/../lib/cucumber/cli/ main.rb:25:in `execute' /Library/Ruby/Gems/1.8/gems/cucumber-0.3.11/bin/cucumber:9 /usr/bin/cucumber:19:in `load' /usr/bin/cucumber:19 From tom at experthuman.com Tue Jul 21 15:58:22 2009 From: tom at experthuman.com (Tom Stuart) Date: Tue, 21 Jul 2009 20:58:22 +0100 Subject: [rspec-users] Loading (or stubbing) data before model loading Message-ID: Hi, A Rails project I'm working on has, for better or worse, some data- driven structure in its models: a few model classes need certain values (e.g. some enumerations) to be available in the database at load time in order for certain bits of metaprogramming to kick off correctly. It would be possible to do this all lazily instead of when the classes load, but in reality the database values are always there and everything already works beautifully so it's hard to justify putting in the effort required to be that lazy. The problem is that RSpec starts with an empty test database and I can't see the best way or place to prepopulate it before the models get loaded (i.e. when Rails bootstraps, since config.cache_classes = true), ideally so that both rake spec and script/spec do the correct setup in time. It would be incredibly convenient to be able to use Fixtures.create_fixtures to do the loading so that the required data can live in YAML fixtures, but maybe it's optimistic to try to do that before Rails has loaded. Or maybe I'm just going about this completely the wrong way, and someone has a clever idea about what the right way is? That would be brilliant. Cheers, -Tom From chris at silhouettesolutions.net Tue Jul 21 19:02:58 2009 From: chris at silhouettesolutions.net (internetchris) Date: Tue, 21 Jul 2009 16:02:58 -0700 (PDT) Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: <16720437-4bdd-4552-8c22-58bc523be059@c29g2000yqd.googlegroups.com> References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> <16720437-4bdd-4552-8c22-58bc523be059@c29g2000yqd.googlegroups.com> Message-ID: I have one additional question after thinking about this for a while. Forgive me if I'm about to read this in the book, It came to mind so I thought I would ask. When writing specs for controllers am I always going to mock the model? Are controllers always isolated from the actual model? If that's the case then using cucumber with webrat is the only time I will test the complete stack right? Just wondering if I understand this correctly. Thanks Chris On Jul 21, 9:25?am, internetchris wrote: > Ben that worked perfectly I appreciate the help. > > Stephen, I appreciate the encouragement, it feels daunting to be > learning all of this at once, but each day I bite off a little more > understanding. ?It's funny you mention scuba diving - "way back when" > right out of high school I thought it would be fun to go to school for > underwater construction/welding - so I did. I suppose if I was able to > tackle that, I will eventually get this. I'm thankful I decided to > switch my degree path to computer science after that however. It was > an interesting part of my life to say the least. Maybe it's a > personality quirk of mine :-) > > Thanks! > > Chris > > On Jul 20, 9:59?pm, Stephen Eley wrote: > > > > > On Mon, Jul 20, 2009 at 11:05 PM, Chris > > > Sund wrote: > > > > I am finally to the point in the rspec book where it describes how to > > > implement controller specs, so I thought what the heck, I'll give it a > > > try. > > > Heh. ?Points to you for ambition! ?You might not have realized this > > (because the RSpec book makes it sound like it should *all* be easy) > > but starting with a Rails controller spec for your very first spec is > > a bit like saying "I was thinking of getting scuba certified... > > Ooooh, CAVE DIVING! ?Let's start with that first!"* > > > On the upside, if you start there and really get to *understand* > > what's going on, you should be relieved when most of the rest of it is > > pretty smooth sailing. ?There are very few common Ruby idioms that > > have such tight coupling as Rails controllers, so very few tasks are > > so hard to spec in isolation. ?You'll almost never have to mock > > anything else as ferociously. ?Model specs in particular will feel > > like sunshine and puppies. > > > So go you! > > > -- > > Have Fun, ?*(Granted, I don't think anyone has ever kicked up silt > > while writing a controller spec, lost hold of their guide line, gotten > > hopelessly lost, and died many hours later in the cold and dark. ?Yet. > > ?That only happens to J2EE programmers.) > > ? ?Steve Eley (sfe... at gmail.com) > > ? ?ESCAPE POD - The Science Fiction Podcast Magazine > > ? ?http://www.escapepod.org > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From ben at benmabey.com Tue Jul 21 21:55:00 2009 From: ben at benmabey.com (Ben Mabey) Date: Tue, 21 Jul 2009 19:55:00 -0600 Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> <16720437-4bdd-4552-8c22-58bc523be059@c29g2000yqd.googlegroups.com> Message-ID: <4A667174.2000901@benmabey.com> internetchris wrote: > I have one additional question after thinking about this for a while. > Forgive me if I'm about to read this in the book, It came to mind so I > thought I would ask. When writing specs for controllers am I always > going to mock the model? Are controllers always isolated from the > actual model? If that's the case then using cucumber with webrat is > the only time I will test the complete stack right? Just wondering if > I understand this correctly. > When I am using Cucumber and writing controller specs that is always how *I* do it. I like having the isolation for the controllers and keeping them fast. Any integration problems will be exposed by Cucumber so I think having the controller specs test more that the controller is a waste in most cases. This is just my opinion however. People on this list will agree with me and disagree, so don't think that it is *the* way of doing things. This is what the book recommends IIRC so you are understanding it. -Ben > Thanks > > Chris > > On Jul 21, 9:25 am, internetchris > wrote: > >> Ben that worked perfectly I appreciate the help. >> >> Stephen, I appreciate the encouragement, it feels daunting to be >> learning all of this at once, but each day I bite off a little more >> understanding. It's funny you mention scuba diving - "way back when" >> right out of high school I thought it would be fun to go to school for >> underwater construction/welding - so I did. I suppose if I was able to >> tackle that, I will eventually get this. I'm thankful I decided to >> switch my degree path to computer science after that however. It was >> an interesting part of my life to say the least. Maybe it's a >> personality quirk of mine :-) >> >> Thanks! >> >> Chris >> >> On Jul 20, 9:59 pm, Stephen Eley wrote: >> >> >> >> >>> On Mon, Jul 20, 2009 at 11:05 PM, Chris >>> >>> Sund wrote: >>> >>>> I am finally to the point in the rspec book where it describes how to >>>> implement controller specs, so I thought what the heck, I'll give it a >>>> try. >>>> >>> Heh. Points to you for ambition! You might not have realized this >>> (because the RSpec book makes it sound like it should *all* be easy) >>> but starting with a Rails controller spec for your very first spec is >>> a bit like saying "I was thinking of getting scuba certified... >>> Ooooh, CAVE DIVING! Let's start with that first!"* >>> >>> On the upside, if you start there and really get to *understand* >>> what's going on, you should be relieved when most of the rest of it is >>> pretty smooth sailing. There are very few common Ruby idioms that >>> have such tight coupling as Rails controllers, so very few tasks are >>> so hard to spec in isolation. You'll almost never have to mock >>> anything else as ferociously. Model specs in particular will feel >>> like sunshine and puppies. >>> >>> So go you! >>> >>> -- >>> Have Fun, *(Granted, I don't think anyone has ever kicked up silt >>> while writing a controller spec, lost hold of their guide line, gotten >>> hopelessly lost, and died many hours later in the cold and dark. Yet. >>> That only happens to J2EE programmers.) >>> Steve Eley (sfe... at gmail.com) >>> ESCAPE POD - The Science Fiction Podcast Magazine >>> http://www.escapepod.org >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users >>> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From sfeley at gmail.com Tue Jul 21 23:43:35 2009 From: sfeley at gmail.com (Stephen Eley) Date: Tue, 21 Jul 2009 23:43:35 -0400 Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> <16720437-4bdd-4552-8c22-58bc523be059@c29g2000yqd.googlegroups.com> Message-ID: <1fb4df0907212043t279cbeacu26777b29d332bb@mail.gmail.com> 2009/7/21 internetchris : > When writing specs for controllers am I always > going to mock the model? Are controllers always isolated from the > actual model? Hi Chris, Pretty insightful of you. This is one of those philosophical questions that tends to keep coming up. The only valid answer to it is "YMMV" -- everyone seems to have a different comfort level with how this is done. David Chelimsky advocates strong isolation and full model mocking for controller specs. That's why you see it in the RSpec book and the official RSpec site. Yehuda Katz pushes a more integrated approach of spec'ing the *request* from soup to nuts rather than the controller's code base; you can see that in Merb's testing helpers, or in this video from last year's RubyConf: http://rubyconf2008.confreaks.com/writing-code-that-doesnt-suck.html Both schools of thought have pros and cons, and both camps have their followers. And then some people take an approach somewhere in the middle, or use Cucumber exclusively, or use macro frameworks like Shoulda or mock_resourceful, or do their own thing, or just throw up their hands and don't test controller flow at all. Having said that -- I'll tell you what *I* do. I used to spec everything. When I was learning RSpec I was quite zealous about it, and I think that was a good experence. These days, though, I skip specs for most of my controllers and declare the integration in Cucumber features instead. I won't say that I *never* write controller specs, but if I do it's usually because that controller does something unusual. Maybe it pulls in a couple of different models, or it needs to reply to "/login" instead of the default "person_sessions/new" or it has a filter to load tabs. I'll spec that sort of interesting behavior. If it's just a straight, looks-like-scaffold-code REST controller, I usually don't spec it. And I certainly don't write stubs for it. My typical feeling is that all that stubbing to set up controller isolation is a lot of work for small risk. 90% of all Rails controllers are pretty nearly identical to each other, and the "cookie cutter" RESTful controller model is simple, well-understood and thoroughly tested in the Rails core code. If it does break on me it's probably because I made some sort of dependency error, like forgetting to declare the route or using the wrong path in my views. That sort of thing turns up in my integration testing (i.e. my Cucumber features) and is always pretty easy to spot and fix. I don't really gain anything by writing specs for generic execution paths that are already tested in Rails itself. That's my approach. I'm not going to claim it's the _best_ -- just that it's where my comfort level is right now. The way I do things has changed before, and it will probably change again. You'll find your own way of doing things too; and that's a Good Thing. In the meantime, you can't really lose by following the steps from the RSpec Book as you're starting out. Even if you do decide that it's too much overhead later on, having had the practice and developed the discipline will help you to make smart decisions and develop your own effective style. -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From sfeley at gmail.com Wed Jul 22 00:06:51 2009 From: sfeley at gmail.com (Stephen Eley) Date: Wed, 22 Jul 2009 00:06:51 -0400 Subject: [rspec-users] [RAILS] - 2.3.3 creating a sea of red In-Reply-To: References: Message-ID: <1fb4df0907212106s77e277c4o252ae7299afc25d6@mail.gmail.com> 2009/7/21 mpd : > > I'm sure this is due to some Rails change, but I can't find any info > about it. Only running my specs is broken, both via rake and the spec > command. I can use script/console with both test and development > environments with no problems, and the app looks to be running fine. > It just looks like a load-order problem somewhere, but I'm not sure > where to start debugging. I can't really help, except to say that I'm also running Rails 2.3.3 and I'm *not* seeing this error. I'm not running fixtures, however (I use Machinist) so that might help you pin it down. Also, grepping for the "abstract_class?" method shows that it's defined in ActiveRecord::Base. You might try diffing between 2.3.2 and 2.3.3 on whatever code loads fixtures for test cases; it might be that 2.3.3 does fixtures a little bit differently or something. -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From chris at silhouettesolutions.net Wed Jul 22 00:36:50 2009 From: chris at silhouettesolutions.net (internetchris) Date: Tue, 21 Jul 2009 21:36:50 -0700 (PDT) Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: <1fb4df0907212043t279cbeacu26777b29d332bb@mail.gmail.com> References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> <16720437-4bdd-4552-8c22-58bc523be059@c29g2000yqd.googlegroups.com> <1fb4df0907212043t279cbeacu26777b29d332bb@mail.gmail.com> Message-ID: <9c0ebd2f-80ce-4a9c-baf8-5308070d4eea@c14g2000yqm.googlegroups.com> Hey guys, I appreciate the insight, and this brings me to my next question, and actually the next chapter in the book (specing models). I read that "most" developers shy away from view specs (for the most part - not always), and then if I understand both of you correctly....each of you tend to "do as little" with controller specs as absolutely necessary. So if that's the case is the main use of rspec within a rails app for the model and validation testing? I'm sure I will gain a little more insight on this as I progress into the next chapter, but it begged to be asked. In fact it's probably too much of a sweeping statement. As I think about this a little more I can see how each of these "little" things that rspec is used for add up to some major tools to help ensure good solid code. Just the re-factoring element which is more a philosophy than it is an actual syntactical element makes for nice DRY systems. I guess this is more of a rhetorical post, but if someone wants to add their two cents go ahead. I have been taking my time through the Rspec book making sure I understand each element. I'm forcing myself to "really" understand it so I can develop better apps. Thanks again! Chris On Jul 21, 9:43?pm, Stephen Eley wrote: > 2009/7/21 internetchris : > > > When writing specs for controllers am I always > > going to mock the model? Are controllers always isolated from the > > actual model? > > Hi Chris, > > Pretty insightful of you. ?This is one of those philosophical > questions that tends to keep coming up. ?The only valid answer to it > is "YMMV" -- everyone seems to have a different comfort level with how > this is done. > > David Chelimsky advocates strong isolation and full model mocking for > controller specs. ?That's why you see it in the RSpec book and the > official RSpec site. ?Yehuda Katz pushes a more integrated approach of > spec'ing the *request* from soup to nuts rather than the controller's > code base; you can see that in Merb's testing helpers, or in this > video from last year's RubyConf:http://rubyconf2008.confreaks.com/writing-code-that-doesnt-suck.html > > Both schools of thought have pros and cons, and both camps have their > followers. ?And then some people take an approach somewhere in the > middle, or use Cucumber exclusively, or use macro frameworks like > Shoulda or mock_resourceful, or do their own thing, or just throw up > their hands and don't test controller flow at all. > > Having said that -- I'll tell you what *I* do. ?I used to spec > everything. ?When I was learning RSpec I was quite zealous about it, > and I think that was a good experence. These days, though, I skip > specs for most of my controllers and declare the integration in > Cucumber features instead. I won't say that I *never* write controller > specs, but if I do it's usually because that controller does something > unusual. ?Maybe it pulls in a couple of different models, or it needs > to reply to "/login" instead of the default "person_sessions/new" or > it has a filter to load tabs. ?I'll spec that sort of interesting > behavior. ?If it's just a straight, looks-like-scaffold-code REST > controller, I usually don't spec it. ?And I certainly don't write > stubs for it. > > My typical feeling is that all that stubbing to set up controller > isolation is a lot of work for small risk. ?90% of all Rails > controllers are pretty nearly identical to each other, and the "cookie > cutter" RESTful controller model is simple, well-understood and > thoroughly tested in the Rails core code. ?If it does break on me it's > probably because I made some sort of dependency error, like forgetting > to declare the route or using the wrong path in my views. ?That sort > of thing turns up in my integration testing (i.e. my Cucumber > features) and is always pretty easy to spot and fix. ?I don't really > gain anything by writing specs for generic execution paths that are > already tested in Rails itself. > > That's my approach. ?I'm not going to claim it's the _best_ -- just > that it's where my comfort level is right now. ?The way I do things > has changed before, and it will probably change again. ?You'll find > your own way of doing things too; and that's a Good Thing. > > In the meantime, you can't really lose by following the steps from the > RSpec Book as you're starting out. ?Even if you do decide that it's > too much overhead later on, having had the practice and developed the > discipline will help you to make smart decisions and develop your own > effective style. > > -- > Have Fun, > ? ?Steve Eley (sfe... at gmail.com) > ? ?ESCAPE POD - The Science Fiction Podcast Magazine > ? ?http://www.escapepod.org > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From amkirwan at gmail.com Wed Jul 22 00:21:10 2009 From: amkirwan at gmail.com (amkirwan) Date: Tue, 21 Jul 2009 21:21:10 -0700 (PDT) Subject: [rspec-users] collection-based finder methods Message-ID: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> How do I spec this following example from the Agile Rails Book listed below. I am doing a similar thing in my controller and when I attempted to change it to the collection way of doing the find I am unable to get my spec to pass though I know it is working fine as my cucumber features are passing old rails way: def show @order = Order.find(params[:id]) end new rails way collection-based: def show id = params[:id] @order = @user.orders.find(id) rescue redirect_to :action => "index" end From sfeley at gmail.com Wed Jul 22 01:15:35 2009 From: sfeley at gmail.com (Stephen Eley) Date: Wed, 22 Jul 2009 01:15:35 -0400 Subject: [rspec-users] My very fist spec file (not sure what's wrong..) In-Reply-To: <9c0ebd2f-80ce-4a9c-baf8-5308070d4eea@c14g2000yqm.googlegroups.com> References: <381c5397-1df9-4830-b07e-19364fd8324d@h31g2000yqd.googlegroups.com> <1fb4df0907202059g38d5a900u2c996adea177acb9@mail.gmail.com> <16720437-4bdd-4552-8c22-58bc523be059@c29g2000yqd.googlegroups.com> <1fb4df0907212043t279cbeacu26777b29d332bb@mail.gmail.com> <9c0ebd2f-80ce-4a9c-baf8-5308070d4eea@c14g2000yqm.googlegroups.com> Message-ID: <1fb4df0907212215i3b0aabf6v60b6aa3ea106f339@mail.gmail.com> 2009/7/22 internetchris : > > So if that's the case is the main use of rspec within a rails app for > the model and validation testing? For a lot of people, yes. It's certainly a fair generalization that model specs are the easiest and most straightforward specs in Rails. If you're applying MVC well, most of the "interesting" stuff should be in the models anyway -- that's where your business logic ought to live. The fundamental work that gives your app a reason to exist. It so happens that business rules are also easier to specify than HTML interfaces (views) or interface setup and dispatching code (controllers). So it tends to attract more testing focus. There are a lot of ways to strike a good balance, though. And adding Cucumber and Webrat (and maybe Selenium, Celerity, Javascript test frameworks, etc.) to the mix makes for even more variations. "There are nine-and sixty ways of constructing tribal lays..." Best of luck to you. Have Fun, ("And--every--single--one--of--them--is--RIGHT!") Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From matt at mattwynne.net Wed Jul 22 07:50:16 2009 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 22 Jul 2009 12:50:16 +0100 Subject: [rspec-users] how i can spec session variable in controller spec? In-Reply-To: <6c9653a80907212336n6cffe7d7m5c05c3a08e90045b@mail.gmail.com> References: <6c9653a80907212336n6cffe7d7m5c05c3a08e90045b@mail.gmail.com> Message-ID: <3CB96070-E4A2-4219-B37B-38628E779989@mattwynne.net> On 22 Jul 2009, at 07:36, Dhwani Panchal wrote: > Hello There, > > Can i spec session variable in my controller spec? Yes: http://rspec.info/rails/writing/controllers.html cheers, Matt Wynne http://mattwynne.net +447974 430184 From matt at mattwynne.net Wed Jul 22 08:13:48 2009 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 22 Jul 2009 13:13:48 +0100 Subject: [rspec-users] Loading (or stubbing) data before model loading In-Reply-To: References: Message-ID: On 21 Jul 2009, at 20:58, Tom Stuart wrote: > Hi, > > A Rails project I'm working on has, for better or worse, some data- > driven structure in its models: a few model classes need certain > values (e.g. some enumerations) to be available in the database at > load time in order for certain bits of metaprogramming to kick off > correctly. It would be possible to do this all lazily instead of > when the classes load, but in reality the database values are always > there and everything already works beautifully so it's hard to > justify putting in the effort required to be that lazy. > > The problem is that RSpec starts with an empty test database and I > can't see the best way or place to prepopulate it before the models > get loaded (i.e. when Rails bootstraps, since config.cache_classes = > true), ideally so that both rake spec and script/spec do the correct > setup in time. It would be incredibly convenient to be able to use > Fixtures.create_fixtures to do the loading so that the required data > can live in YAML fixtures, but maybe it's optimistic to try to do > that before Rails has loaded. > > Or maybe I'm just going about this completely the wrong way, and > someone has a clever idea about what the right way is? That would be > brilliant. One challenge for you before we dig into how to solve it the way you've suggested: do you really need to keep these enumerables in the database, or could you keep them in code? That is, is there a valid use case where they will be altered at run-time, or do they change so seldom that it would be OK to have to release a new version of the code when they need to be changed? I ask because these things tend to get a lot easier if you're not having to mess around with databases, so it's always worth pushing back on that one first. cheers, Matt Wynne http://mattwynne.net +447974 430184 From dchelimsky at gmail.com Wed Jul 22 08:57:59 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 07:57:59 -0500 Subject: [rspec-users] Loading (or stubbing) data before model loading In-Reply-To: References: Message-ID: <57c63afe0907220557o2e9475e3ra80f5ce109550693@mail.gmail.com> On Tue, Jul 21, 2009 at 2:58 PM, Tom Stuart wrote: > Hi, > > A Rails project I'm working on has, for better or worse, some data-driven > structure in its models: a few model classes need certain values (e.g. some > enumerations) to be available in the database at load time in order for > certain bits of metaprogramming to kick off correctly. It would be possible > to do this all lazily instead of when the classes load, but in reality the > database values are always there and everything already works beautifully so > it's hard to justify putting in the effort required to be that lazy. > > The problem is that RSpec starts with an empty test database Not exactly. RSpec starts with whatever database you have and rolls back to that state after each example provided you're only using before(:each) to set up state before each example. Any pre-existing database state will remain, and any state that you set up in before(:suite) or before(:all) will not be rolled back unless you do so explicitly. HTH, David > and I can't see > the best way or place to prepopulate it before the models get loaded (i.e. > when Rails bootstraps, since config.cache_classes = true), ideally so that > both rake spec and script/spec do the correct setup in time. It would be > incredibly convenient to be able to use Fixtures.create_fixtures to do the > loading so that the required data can live in YAML fixtures, but maybe it's > optimistic to try to do that before Rails has loaded. > > Or maybe I'm just going about this completely the wrong way, and someone has > a clever idea about what the right way is? That would be brilliant. > > Cheers, > -Tom > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Wed Jul 22 09:13:59 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 08:13:59 -0500 Subject: [rspec-users] collection-based finder methods In-Reply-To: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> Message-ID: <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > How do I spec this following example from the Agile Rails Book listed > below. I am doing a similar thing in my controller and when I > attempted to change it to the collection way of doing the find I am > unable to get my spec to pass though I know it is working fine as my > cucumber features are passing > > old rails way: > > def show > @order = Order.find(params[:id]) > end > > new rails way collection-based: > > def show > id = params[:id] > @order = @user.orders.find(id) This code is inherently untestable in an isolated/granular way. Your options are: * write higher level specs that use real data * pros: simplicity and clarity in both code and specs * cons: brittle due to runtime dependency on correct models, runs slow * write a very invasive spec with complex setup and instance_eval to set up the @user * pros: runs fast, no runtime dependency on correct models * cons: brittle due to dependency on internals, complex * refactor the code to make it easier to spec * pros: more highly decoupled code, simpler specs, fast * cons: more work up front, may disregard some of what Rails has to offer Note that the first two options are both brittle, but for different reasons. The first is brittle due to a runtime dependency. That means that when you run the spec the model has to be working correctly for the spec to pass, and a failure could be due to a problem in the model or in the controller. The second is due to a code dependency. That means that when you want to change this code, the spec will have to change as well. This is true of any case in which you use mocks or stubs to varying degrees, and that comes with its own tradeoffs. In this case, the necessary stubbing would be complex and invasive enough that it would be a concern to me. Getting to your original question - what does your spec look like now, and what failure message are you getting? Cheers, David > rescue > redirect_to :action => "index" > end > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From tom at experthuman.com Wed Jul 22 09:27:52 2009 From: tom at experthuman.com (Tom Stuart) Date: Wed, 22 Jul 2009 14:27:52 +0100 Subject: [rspec-users] Loading (or stubbing) data before model loading In-Reply-To: <57c63afe0907220557o2e9475e3ra80f5ce109550693@mail.gmail.com> References: <57c63afe0907220557o2e9475e3ra80f5ce109550693@mail.gmail.com> Message-ID: <0EFB6640-7BDA-43C4-9C9C-9C5D84D9A832@experthuman.com> On 22 Jul 2009, at 13:57, David Chelimsky wrote: >> The problem is that RSpec starts with an empty test database > Not exactly. RSpec starts with whatever database you have and rolls > back to that state after each example provided you're only using > before(:each) to set up state before each example. Quite right, and I should've been more specific: rake spec sets up an empty database and then runs RSpec. In the end I solved my problem by extending the db:test:prepare task to load the fixtures after cloning the development database structure, which in hindsight is obviously the right way to fix it. > Any pre-existing database state will remain, and any state that you > set up in before(:suite) or before(:all) will not be rolled back > unless you do so explicitly. On an unrelated note, why do I occasionally see leakage of fixture data between specs? I'd expect RSpec's explicit state rollback, plus Rails' transactional fixtures, to mean that fixtures loaded in one example would always be invisible to another, but that doesn't seem to be the case; I've seen many instances of mysteriously-appearing spec failures which turned out to be because of a change in running order (touching files + --loadby mtime --reverse) causing fixture leakage to move somewhere else, always easily remedied by explicitly loading whatever fixture had been forgotten. Is this a bug somewhere in RSpec or Rails (or MySQL), or something I'm just doing wrong? Cheers, -Tom From dchelimsky at gmail.com Wed Jul 22 09:44:22 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 08:44:22 -0500 Subject: [rspec-users] Loading (or stubbing) data before model loading In-Reply-To: <0EFB6640-7BDA-43C4-9C9C-9C5D84D9A832@experthuman.com> References: <57c63afe0907220557o2e9475e3ra80f5ce109550693@mail.gmail.com> <0EFB6640-7BDA-43C4-9C9C-9C5D84D9A832@experthuman.com> Message-ID: <57c63afe0907220644t3d1c3467id7721c92400d25c8@mail.gmail.com> On Wed, Jul 22, 2009 at 8:27 AM, Tom Stuart wrote: > On 22 Jul 2009, at 13:57, David Chelimsky wrote: >>> >>> The problem is that RSpec starts with an empty test database >> >> Not exactly. RSpec starts with whatever database you have and rolls >> back to that state after each example provided you're only using >> before(:each) to set up state before each example. > > Quite right, and I should've been more specific: rake spec sets up an empty > database and then runs RSpec. In the end I solved my problem by extending > the db:test:prepare task to load the fixtures after cloning the development > database structure, which in hindsight is obviously the right way to fix it. > >> Any pre-existing database state will remain, and any state that you >> set up in before(:suite) or before(:all) will not be rolled back >> unless you do so explicitly. > > On an unrelated note, why do I occasionally see leakage of fixture data > between specs? I'd expect RSpec's explicit state rollback There is no such thing as RSpec's explicit state rollback, thought that does sound quite fancy. All RSpec does is create new instances of each example group to run each example in. That means that any local state does not get copied from one example to another. You are responsible for managing any global state. The same is true for any other testing framework btw (at least those that I know of). > , plus Rails' > transactional fixtures, to mean that fixtures loaded in one example would > always be invisible to another, but that doesn't seem to be the case; I've > seen many instances of mysteriously-appearing spec failures which turned out > to be because of a change in running order (touching files + --loadby mtime > --reverse) causing fixture leakage to move somewhere else, always easily > remedied by explicitly loading whatever fixture had been forgotten. Is this > a bug somewhere in RSpec or Rails (or MySQL), or something I'm just doing > wrong? I can't speak for your situation in specific, but here are some possible causes: * setting up state in a before(:all) This data is not rolled back after the example group is run, so you have to explicitly roll it back in an after(:all). If other specs were passing due to state set up in a before(:all) that wasn't properly matched with an after(:all) cleanup and happened to run before those passing specs, then running those specs in isolation, or before the offending group (with the before(:all)) may cause them to fail. * data-backed global variables This may seem counter-intuitive, since the database is rolled back after each example - but keep in mind that _only_ the database is rollled back. If a data-backed model is loaded into memory in a global variable, that object is still there, even if the record in the database no longer exists. HTH, David > Cheers, > -Tom From amkirwan at gmail.com Wed Jul 22 10:12:46 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 07:12:46 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> Message-ID: <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> My spec is a messed up because I have tried everything I can think of to mock but this is what I have for the show method. The @user instance is setup in the login_and_before_filter_pass macros with the following: @user = mock_model(Person, :null_object => true) The error I keep receiving is that assigns[:letter].should equal @letter keeps return that it is expecting a Person object instead of a Letter object. The only way I can get it to pass is by putting @user.letters.should_receive(:find).with("1").and_return(@letter) directly in the "should assign the found letter for the view" I feel like I must be missing something about how stubbing and mocking work # Get /admin/letters/1 def show id = params[:id] @letter = @user.letters.find(id) end describe Admin::LettersController, "SHOW GET /admin/letters/1" do before(:each) do @user.letters.should_receive(:find).with("1").and_return(@letter) end def do_get put :show, {:id => "1"}, @session end login_and_before_filter_pass(:filter => :admin_only, :request_method => :get, :action => :show, :parameters => {:cas_user => 'ak730'}) it "should be successful" do do_get response.should be_success end it "should find the letter requested" do @user.letters.should_receive(:find).with("1").and_return(@letter) puts(@letter) do_get end it "should assign the found letter for the view" do # uncommenting will allow to pass # @user.letters.should_receive(:find).with("1").and_return (@letter) do_get assigns[:letter].should equal(@letter) end it "should render show template" do do_get response.should render_template("show") end end On Jul 22, 9:13?am, David Chelimsky wrote: > On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > > How do I spec this following example from the Agile Rails Book listed > > below. I am doing a similar thing in my controller and when I > > attempted to change it to the collection way of doing the find I am > > unable to get my spec to pass though I know it is working fine as my > > cucumber features are passing > > > old rails way: > > > def show > > @order = Order.find(params[:id]) > > end > > > new rails way collection-based: > > > def show > > id = params[:id] > > @order = @user.orders.find(id) > > This code is inherently untestable in an isolated/granular way. Your > options are: > > * write higher level specs that use real data > ? * pros: simplicity and clarity in both code and specs > ? * cons: brittle due to runtime dependency on correct models, runs slow > > * write a very invasive spec with complex setup and instance_eval to > set up the @user > ? * pros: runs fast, no runtime dependency on correct models > ? * cons: brittle due to dependency on internals, complex > > * refactor the code to make it easier to spec > ? * pros: more highly decoupled code, simpler specs, fast > ? * cons: more work up front, may disregard some of what Rails has to offer > > Note that the first two options are both brittle, but for different > reasons. The first is brittle due to a runtime dependency. That means > that when you run the spec the model has to be working correctly for > the spec to pass, and a failure could be due to a problem in the model > or in the controller. > > The second is due to a code dependency. That means that when you want > to change this code, the spec will have to change as well. This is > true of any case in which you use mocks or stubs to varying degrees, > and that comes with its own tradeoffs. In this case, the necessary > stubbing would be complex and invasive enough that it would be a > concern to me. > > Getting to your original question - what does your spec look like now, > and what failure message are you getting? > > Cheers, > David > > > rescue > > redirect_to :action => "index" > > end > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Wed Jul 22 10:42:13 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 09:42:13 -0500 Subject: [rspec-users] collection-based finder methods In-Reply-To: <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> Message-ID: <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > My spec is a messed up because I have tried everything I can think of > to mock but this is what I have for the show method. The @user > instance is setup in the login_and_before_filter_pass macros with the > following: @user = mock_model(Person, :null_object => true) > > The error I keep receiving is that assigns[:letter].should equal > @letter keeps return that it is expecting a Person object instead of a > Letter object. The only way I can get it to pass is by putting > @user.letters.should_receive(:find).with("1").and_return(@letter) > directly in the "should assign the found letter for the view" > > I feel like I must be missing something about how stubbing and mocking > work > > > ?# Get /admin/letters/1 > ?def show > ? ?id = params[:id] > ? ?@letter = ?@user.letters.find(id) > ?end > > > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > ?before(:each) do > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) This @user is an instance variable in the spec, and is not the same @user that is in the controller. HTH, David > ?end > > ?def do_get > ? ?put :show, {:id => "1"}, @session > ?end > > ?login_and_before_filter_pass(:filter => :admin_only, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > ?it "should be successful" do > ? ?do_get > ? ?response.should be_success > ?end > > ?it "should find the letter requested" do > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > ? ?puts(@letter) > ? ?do_get > ?end > > ?it "should assign the found letter for the view" do > ? ?# uncommenting will allow to pass > ? ?# @user.letters.should_receive(:find).with("1").and_return > (@letter) > ? ?do_get > ? ?assigns[:letter].should equal(@letter) > ?end > > ?it "should render show template" do > ? ?do_get > ? ?response.should render_template("show") > ?end > > end > > > > On Jul 22, 9:13?am, David Chelimsky wrote: >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: >> > How do I spec this following example from the Agile Rails Book listed >> > below. I am doing a similar thing in my controller and when I >> > attempted to change it to the collection way of doing the find I am >> > unable to get my spec to pass though I know it is working fine as my >> > cucumber features are passing >> >> > old rails way: >> >> > def show >> > @order = Order.find(params[:id]) >> > end >> >> > new rails way collection-based: >> >> > def show >> > id = params[:id] >> > @order = @user.orders.find(id) >> >> This code is inherently untestable in an isolated/granular way. Your >> options are: >> >> * write higher level specs that use real data >> ? * pros: simplicity and clarity in both code and specs >> ? * cons: brittle due to runtime dependency on correct models, runs slow >> >> * write a very invasive spec with complex setup and instance_eval to >> set up the @user >> ? * pros: runs fast, no runtime dependency on correct models >> ? * cons: brittle due to dependency on internals, complex >> >> * refactor the code to make it easier to spec >> ? * pros: more highly decoupled code, simpler specs, fast >> ? * cons: more work up front, may disregard some of what Rails has to offer >> >> Note that the first two options are both brittle, but for different >> reasons. The first is brittle due to a runtime dependency. That means >> that when you run the spec the model has to be working correctly for >> the spec to pass, and a failure could be due to a problem in the model >> or in the controller. >> >> The second is due to a code dependency. That means that when you want >> to change this code, the spec will have to change as well. This is >> true of any case in which you use mocks or stubs to varying degrees, >> and that comes with its own tradeoffs. In this case, the necessary >> stubbing would be complex and invasive enough that it would be a >> concern to me. >> >> Getting to your original question - what does your spec look like now, >> and what failure message are you getting? >> >> Cheers, >> David >> >> > rescue >> > redirect_to :action => "index" >> > end >> > _______________________________________________ >> > rspec-users mailing list >> > rspec-us... at rubyforge.org >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From amkirwan at gmail.com Wed Jul 22 11:31:48 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 08:31:48 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> Message-ID: Thanks for the help but I guess I am not getting something. How is @user= = mock_model(Person) and different then the following code: message = mock_model(Message) Message.stub!(:new).and_return message message.should_receive(:save) post :create def create message = Message.new params[:new] message.save end I guess I don't understand why assigns[:letter] is expecting a Person instance instead of a Letter instance On Jul 22, 10:42?am, David Chelimsky wrote: > On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > > My spec is a messed up because I have tried everything I can think of > > to mock but this is what I have for the show method. The @user > > instance is setup in the login_and_before_filter_pass macros with the > > following: @user = mock_model(Person, :null_object => true) > > > The error I keep receiving is that assigns[:letter].should equal > > @letter keeps return that it is expecting a Person object instead of a > > Letter object. The only way I can get it to pass is by putting > > @user.letters.should_receive(:find).with("1").and_return(@letter) > > directly in the "should assign the found letter for the view" > > > I feel like I must be missing something about how stubbing and mocking > > work > > > ?# Get /admin/letters/1 > > ?def show > > ? ?id = params[:id] > > ? ?@letter = ?@user.letters.find(id) > > ?end > > > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > > ?before(:each) do > > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > This @user is an instance variable in the spec, and is not the same > @user that is in the controller. > > HTH, > David > > > > > > > ?end > > > ?def do_get > > ? ?put :show, {:id => "1"}, @session > > ?end > > > ?login_and_before_filter_pass(:filter => :admin_only, > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > > ?it "should be successful" do > > ? ?do_get > > ? ?response.should be_success > > ?end > > > ?it "should find the letter requested" do > > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > ? ?puts(@letter) > > ? ?do_get > > ?end > > > ?it "should assign the found letter for the view" do > > ? ?# uncommenting will allow to pass > > ? ?# @user.letters.should_receive(:find).with("1").and_return > > (@letter) > > ? ?do_get > > ? ?assigns[:letter].should equal(@letter) > > ?end > > > ?it "should render show template" do > > ? ?do_get > > ? ?response.should render_template("show") > > ?end > > > end > > > On Jul 22, 9:13?am, David Chelimsky wrote: > >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > >> > How do I spec this following example from the Agile Rails Book listed > >> > below. I am doing a similar thing in my controller and when I > >> > attempted to change it to the collection way of doing the find I am > >> > unable to get my spec to pass though I know it is working fine as my > >> > cucumber features are passing > > >> > old rails way: > > >> > def show > >> > @order = Order.find(params[:id]) > >> > end > > >> > new rails way collection-based: > > >> > def show > >> > id = params[:id] > >> > @order = @user.orders.find(id) > > >> This code is inherently untestable in an isolated/granular way. Your > >> options are: > > >> * write higher level specs that use real data > >> ? * pros: simplicity and clarity in both code and specs > >> ? * cons: brittle due to runtime dependency on correct models, runs slow > > >> * write a very invasive spec with complex setup and instance_eval to > >> set up the @user > >> ? * pros: runs fast, no runtime dependency on correct models > >> ? * cons: brittle due to dependency on internals, complex > > >> * refactor the code to make it easier to spec > >> ? * pros: more highly decoupled code, simpler specs, fast > >> ? * cons: more work up front, may disregard some of what Rails has to offer > > >> Note that the first two options are both brittle, but for different > >> reasons. The first is brittle due to a runtime dependency. That means > >> that when you run the spec the model has to be working correctly for > >> the spec to pass, and a failure could be due to a problem in the model > >> or in the controller. > > >> The second is due to a code dependency. That means that when you want > >> to change this code, the spec will have to change as well. This is > >> true of any case in which you use mocks or stubs to varying degrees, > >> and that comes with its own tradeoffs. In this case, the necessary > >> stubbing would be complex and invasive enough that it would be a > >> concern to me. > > >> Getting to your original question - what does your spec look like now, > >> and what failure message are you getting? > > >> Cheers, > >> David > > >> > rescue > >> > redirect_to :action => "index" > >> > end > >> > _______________________________________________ > >> > rspec-users mailing list > >> > rspec-us... at rubyforge.org > >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Wed Jul 22 11:39:08 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 10:39:08 -0500 Subject: [rspec-users] collection-based finder methods In-Reply-To: References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> Message-ID: <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: > Thanks for the help but I guess I am not getting something. How is > @user= = mock_model(Person) and different then the following code: This is assigning the mock_model(Person) to a @user instance variable in the spec. This is not the same user that is in the controller. > message = mock_model(Message) > Message.stub!(:new).and_return message > message.should_receive(:save) > post :create This creates a mock_model(Message) and then tells Message to return it when it receives :new, therefore message in the spec is the same object as Message.new in the controller. > def create > message = Message.new params[:new] > message.save > end > I guess I don't understand why assigns[:letter] is expecting a Person > instance instead of a Letter instance I'm not clear on what you mean by this. Would you please post the full error message (either here or in a pastie or gist)? > > On Jul 22, 10:42?am, David Chelimsky wrote: >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: >> > My spec is a messed up because I have tried everything I can think of >> > to mock but this is what I have for the show method. The @user >> > instance is setup in the login_and_before_filter_pass macros with the >> > following: @user = mock_model(Person, :null_object => true) >> >> > The error I keep receiving is that assigns[:letter].should equal >> > @letter keeps return that it is expecting a Person object instead of a >> > Letter object. The only way I can get it to pass is by putting >> > @user.letters.should_receive(:find).with("1").and_return(@letter) >> > directly in the "should assign the found letter for the view" >> >> > I feel like I must be missing something about how stubbing and mocking >> > work >> >> > ?# Get /admin/letters/1 >> > ?def show >> > ? ?id = params[:id] >> > ? ?@letter = ?@user.letters.find(id) >> > ?end >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do >> >> > ?before(:each) do >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> >> This @user is an instance variable in the spec, and is not the same >> @user that is in the controller. >> >> HTH, >> David >> >> >> >> >> >> > ?end >> >> > ?def do_get >> > ? ?put :show, {:id => "1"}, @session >> > ?end >> >> > ?login_and_before_filter_pass(:filter => :admin_only, >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) >> >> > ?it "should be successful" do >> > ? ?do_get >> > ? ?response.should be_success >> > ?end >> >> > ?it "should find the letter requested" do >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> > ? ?puts(@letter) >> > ? ?do_get >> > ?end >> >> > ?it "should assign the found letter for the view" do >> > ? ?# uncommenting will allow to pass >> > ? ?# @user.letters.should_receive(:find).with("1").and_return >> > (@letter) >> > ? ?do_get >> > ? ?assigns[:letter].should equal(@letter) >> > ?end >> >> > ?it "should render show template" do >> > ? ?do_get >> > ? ?response.should render_template("show") >> > ?end >> >> > end >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: >> >> > How do I spec this following example from the Agile Rails Book listed >> >> > below. I am doing a similar thing in my controller and when I >> >> > attempted to change it to the collection way of doing the find I am >> >> > unable to get my spec to pass though I know it is working fine as my >> >> > cucumber features are passing >> >> >> > old rails way: >> >> >> > def show >> >> > @order = Order.find(params[:id]) >> >> > end >> >> >> > new rails way collection-based: >> >> >> > def show >> >> > id = params[:id] >> >> > @order = @user.orders.find(id) >> >> >> This code is inherently untestable in an isolated/granular way. Your >> >> options are: >> >> >> * write higher level specs that use real data >> >> ? * pros: simplicity and clarity in both code and specs >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow >> >> >> * write a very invasive spec with complex setup and instance_eval to >> >> set up the @user >> >> ? * pros: runs fast, no runtime dependency on correct models >> >> ? * cons: brittle due to dependency on internals, complex >> >> >> * refactor the code to make it easier to spec >> >> ? * pros: more highly decoupled code, simpler specs, fast >> >> ? * cons: more work up front, may disregard some of what Rails has to offer >> >> >> Note that the first two options are both brittle, but for different >> >> reasons. The first is brittle due to a runtime dependency. That means >> >> that when you run the spec the model has to be working correctly for >> >> the spec to pass, and a failure could be due to a problem in the model >> >> or in the controller. >> >> >> The second is due to a code dependency. That means that when you want >> >> to change this code, the spec will have to change as well. This is >> >> true of any case in which you use mocks or stubs to varying degrees, >> >> and that comes with its own tradeoffs. In this case, the necessary >> >> stubbing would be complex and invasive enough that it would be a >> >> concern to me. >> >> >> Getting to your original question - what does your spec look like now, >> >> and what failure message are you getting? >> >> >> Cheers, >> >> David >> >> >> > rescue >> >> > redirect_to :action => "index" >> >> > end >> >> > _______________________________________________ >> >> > rspec-users mailing list >> >> > rspec-us... at rubyforge.org >> >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> >> _______________________________________________ >> >> rspec-users mailing list >> >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ >> > rspec-users mailing list >> > rspec-us... at rubyforge.org >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From amkirwan at gmail.com Wed Jul 22 11:52:07 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 08:52:07 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> Message-ID: Okay I get the "and_return" part now thanks. Here is the error http://gist.github.com/152061 On Jul 22, 11:39?am, David Chelimsky wrote: > On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: > > Thanks for the help but I guess I am not getting something. How is > > @user= = mock_model(Person) and different then the following code: > > This is assigning the mock_model(Person) to a @user instance variable > in the spec. This is not the same user that is in the controller. > > > message = mock_model(Message) > > Message.stub!(:new).and_return message > > message.should_receive(:save) > > post :create > > This creates a mock_model(Message) and then tells Message to return it > when it receives :new, therefore message in the spec is the same > object as Message.new in the controller. > > > def create > > message = Message.new params[:new] > > message.save > > end > > I guess I don't understand why assigns[:letter] is expecting a Person > > instance instead of a Letter instance > > I'm not clear on what you mean by this. Would you please post the full > error message (either here or in a pastie or gist)? > > > > > > > > > On Jul 22, 10:42?am, David Chelimsky wrote: > >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > >> > My spec is a messed up because I have tried everything I can think of > >> > to mock but this is what I have for the show method. The @user > >> > instance is setup in the login_and_before_filter_pass macros with the > >> > following: @user = mock_model(Person, :null_object => true) > > >> > The error I keep receiving is that assigns[:letter].should equal > >> > @letter keeps return that it is expecting a Person object instead of a > >> > Letter object. The only way I can get it to pass is by putting > >> > @user.letters.should_receive(:find).with("1").and_return(@letter) > >> > directly in the "should assign the found letter for the view" > > >> > I feel like I must be missing something about how stubbing and mocking > >> > work > > >> > ?# Get /admin/letters/1 > >> > ?def show > >> > ? ?id = params[:id] > >> > ? ?@letter = ?@user.letters.find(id) > >> > ?end > > >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > >> > ?before(:each) do > >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > >> This @user is an instance variable in the spec, and is not the same > >> @user that is in the controller. > > >> HTH, > >> David > > >> > ?end > > >> > ?def do_get > >> > ? ?put :show, {:id => "1"}, @session > >> > ?end > > >> > ?login_and_before_filter_pass(:filter => :admin_only, > >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > >> > ?it "should be successful" do > >> > ? ?do_get > >> > ? ?response.should be_success > >> > ?end > > >> > ?it "should find the letter requested" do > >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > >> > ? ?puts(@letter) > >> > ? ?do_get > >> > ?end > > >> > ?it "should assign the found letter for the view" do > >> > ? ?# uncommenting will allow to pass > >> > ? ?# @user.letters.should_receive(:find).with("1").and_return > >> > (@letter) > >> > ? ?do_get > >> > ? ?assigns[:letter].should equal(@letter) > >> > ?end > > >> > ?it "should render show template" do > >> > ? ?do_get > >> > ? ?response.should render_template("show") > >> > ?end > > >> > end > > >> > On Jul 22, 9:13?am, David Chelimsky wrote: > >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > >> >> > How do I spec this following example from the Agile Rails Book listed > >> >> > below. I am doing a similar thing in my controller and when I > >> >> > attempted to change it to the collection way of doing the find I am > >> >> > unable to get my spec to pass though I know it is working fine as my > >> >> > cucumber features are passing > > >> >> > old rails way: > > >> >> > def show > >> >> > @order = Order.find(params[:id]) > >> >> > end > > >> >> > new rails way collection-based: > > >> >> > def show > >> >> > id = params[:id] > >> >> > @order = @user.orders.find(id) > > >> >> This code is inherently untestable in an isolated/granular way. Your > >> >> options are: > > >> >> * write higher level specs that use real data > >> >> ? * pros: simplicity and clarity in both code and specs > >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow > > >> >> * write a very invasive spec with complex setup and instance_eval to > >> >> set up the @user > >> >> ? * pros: runs fast, no runtime dependency on correct models > >> >> ? * cons: brittle due to dependency on internals, complex > > >> >> * refactor the code to make it easier to spec > >> >> ? * pros: more highly decoupled code, simpler specs, fast > >> >> ? * cons: more work up front, may disregard some of what Rails has to offer > > >> >> Note that the first two options are both brittle, but for different > >> >> reasons. The first is brittle due to a runtime dependency. That means > >> >> that when you run the spec the model has to be working correctly for > >> >> the spec to pass, and a failure could be due to a problem in the model > >> >> or in the controller. > > >> >> The second is due to a code dependency. That means that when you want > >> >> to change this code, the spec will have to change as well. This is > >> >> true of any case in which you use mocks or stubs to varying degrees, > >> >> and that comes with its own tradeoffs. In this case, the necessary > >> >> stubbing would be complex and invasive enough that it would be a > >> >> concern to me. > > >> >> Getting to your original question - what does your spec look like now, > >> >> and what failure message are you getting? > > >> >> Cheers, > >> >> David > > >> >> > rescue > >> >> > redirect_to :action => "index" > >> >> > end > >> >> > _______________________________________________ > >> >> > rspec-users mailing list > >> >> > rspec-us... at rubyforge.org > >> >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> >> _______________________________________________ > >> >> rspec-users mailing list > >> >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > >> > _______________________________________________ > >> > rspec-users mailing list > >> > rspec-us... at rubyforge.org > >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From amkirwan at gmail.com Wed Jul 22 11:54:34 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 08:54:34 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> Message-ID: thanks, I understand now the ".and_return" part. Here is the failure I am receiving http://gist.github.com/152061 On Jul 22, 11:39?am, David Chelimsky wrote: > On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: > > Thanks for the help but I guess I am not getting something. How is > > @user= = mock_model(Person) and different then the following code: > > This is assigning the mock_model(Person) to a @user instance variable > in the spec. This is not the same user that is in the controller. > > > message = mock_model(Message) > > Message.stub!(:new).and_return message > > message.should_receive(:save) > > post :create > > This creates a mock_model(Message) and then tells Message to return it > when it receives :new, therefore message in the spec is the same > object as Message.new in the controller. > > > def create > > message = Message.new params[:new] > > message.save > > end > > I guess I don't understand why assigns[:letter] is expecting a Person > > instance instead of a Letter instance > > I'm not clear on what you mean by this. Would you please post the full > error message (either here or in a pastie or gist)? > > > > > > > > > On Jul 22, 10:42?am, David Chelimsky wrote: > >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > >> > My spec is a messed up because I have tried everything I can think of > >> > to mock but this is what I have for the show method. The @user > >> > instance is setup in the login_and_before_filter_pass macros with the > >> > following: @user = mock_model(Person, :null_object => true) > > >> > The error I keep receiving is that assigns[:letter].should equal > >> > @letter keeps return that it is expecting a Person object instead of a > >> > Letter object. The only way I can get it to pass is by putting > >> > @user.letters.should_receive(:find).with("1").and_return(@letter) > >> > directly in the "should assign the found letter for the view" > > >> > I feel like I must be missing something about how stubbing and mocking > >> > work > > >> > ?# Get /admin/letters/1 > >> > ?def show > >> > ? ?id = params[:id] > >> > ? ?@letter = ?@user.letters.find(id) > >> > ?end > > >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > >> > ?before(:each) do > >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > >> This @user is an instance variable in the spec, and is not the same > >> @user that is in the controller. > > >> HTH, > >> David > > >> > ?end > > >> > ?def do_get > >> > ? ?put :show, {:id => "1"}, @session > >> > ?end > > >> > ?login_and_before_filter_pass(:filter => :admin_only, > >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > >> > ?it "should be successful" do > >> > ? ?do_get > >> > ? ?response.should be_success > >> > ?end > > >> > ?it "should find the letter requested" do > >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > >> > ? ?puts(@letter) > >> > ? ?do_get > >> > ?end > > >> > ?it "should assign the found letter for the view" do > >> > ? ?# uncommenting will allow to pass > >> > ? ?# @user.letters.should_receive(:find).with("1").and_return > >> > (@letter) > >> > ? ?do_get > >> > ? ?assigns[:letter].should equal(@letter) > >> > ?end > > >> > ?it "should render show template" do > >> > ? ?do_get > >> > ? ?response.should render_template("show") > >> > ?end > > >> > end > > >> > On Jul 22, 9:13?am, David Chelimsky wrote: > >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > >> >> > How do I spec this following example from the Agile Rails Book listed > >> >> > below. I am doing a similar thing in my controller and when I > >> >> > attempted to change it to the collection way of doing the find I am > >> >> > unable to get my spec to pass though I know it is working fine as my > >> >> > cucumber features are passing > > >> >> > old rails way: > > >> >> > def show > >> >> > @order = Order.find(params[:id]) > >> >> > end > > >> >> > new rails way collection-based: > > >> >> > def show > >> >> > id = params[:id] > >> >> > @order = @user.orders.find(id) > > >> >> This code is inherently untestable in an isolated/granular way. Your > >> >> options are: > > >> >> * write higher level specs that use real data > >> >> ? * pros: simplicity and clarity in both code and specs > >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow > > >> >> * write a very invasive spec with complex setup and instance_eval to > >> >> set up the @user > >> >> ? * pros: runs fast, no runtime dependency on correct models > >> >> ? * cons: brittle due to dependency on internals, complex > > >> >> * refactor the code to make it easier to spec > >> >> ? * pros: more highly decoupled code, simpler specs, fast > >> >> ? * cons: more work up front, may disregard some of what Rails has to offer > > >> >> Note that the first two options are both brittle, but for different > >> >> reasons. The first is brittle due to a runtime dependency. That means > >> >> that when you run the spec the model has to be working correctly for > >> >> the spec to pass, and a failure could be due to a problem in the model > >> >> or in the controller. > > >> >> The second is due to a code dependency. That means that when you want > >> >> to change this code, the spec will have to change as well. This is > >> >> true of any case in which you use mocks or stubs to varying degrees, > >> >> and that comes with its own tradeoffs. In this case, the necessary > >> >> stubbing would be complex and invasive enough that it would be a > >> >> concern to me. > > >> >> Getting to your original question - what does your spec look like now, > >> >> and what failure message are you getting? > > >> >> Cheers, > >> >> David > > >> >> > rescue > >> >> > redirect_to :action => "index" > >> >> > end > >> >> > _______________________________________________ > >> >> > rspec-users mailing list > >> >> > rspec-us... at rubyforge.org > >> >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> >> _______________________________________________ > >> >> rspec-users mailing list > >> >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > >> > _______________________________________________ > >> > rspec-users mailing list > >> > rspec-us... at rubyforge.org > >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Wed Jul 22 12:11:25 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 11:11:25 -0500 Subject: [rspec-users] collection-based finder methods In-Reply-To: References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> Message-ID: <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> On Wed, Jul 22, 2009 at 10:52 AM, amkirwan wrote: > Okay I get the "and_return" part now thanks. > > Here is the error > > http://gist.github.com/152061 Well, the error seems quite clear. It's expecting a Letter and gets a Person. I know you've posted bits of the code in this thread, but would you mind posting the entire spec and controller code in a gist? At least the code related to the show action. Thanks, David > > On Jul 22, 11:39?am, David Chelimsky wrote: >> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: >> > Thanks for the help but I guess I am not getting something. How is >> > @user= = mock_model(Person) and different then the following code: >> >> This is assigning the mock_model(Person) to a @user instance variable >> in the spec. This is not the same user that is in the controller. >> >> > message = mock_model(Message) >> > Message.stub!(:new).and_return message >> > message.should_receive(:save) >> > post :create >> >> This creates a mock_model(Message) and then tells Message to return it >> when it receives :new, therefore message in the spec is the same >> object as Message.new in the controller. >> >> > def create >> > message = Message.new params[:new] >> > message.save >> > end >> > I guess I don't understand why assigns[:letter] is expecting a Person >> > instance instead of a Letter instance >> >> I'm not clear on what you mean by this. Would you please post the full >> error message (either here or in a pastie or gist)? >> >> >> >> >> >> >> >> > On Jul 22, 10:42?am, David Chelimsky wrote: >> >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: >> >> > My spec is a messed up because I have tried everything I can think of >> >> > to mock but this is what I have for the show method. The @user >> >> > instance is setup in the login_and_before_filter_pass macros with the >> >> > following: @user = mock_model(Person, :null_object => true) >> >> >> > The error I keep receiving is that assigns[:letter].should equal >> >> > @letter keeps return that it is expecting a Person object instead of a >> >> > Letter object. The only way I can get it to pass is by putting >> >> > @user.letters.should_receive(:find).with("1").and_return(@letter) >> >> > directly in the "should assign the found letter for the view" >> >> >> > I feel like I must be missing something about how stubbing and mocking >> >> > work >> >> >> > ?# Get /admin/letters/1 >> >> > ?def show >> >> > ? ?id = params[:id] >> >> > ? ?@letter = ?@user.letters.find(id) >> >> > ?end >> >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do >> >> >> > ?before(:each) do >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> >> >> This @user is an instance variable in the spec, and is not the same >> >> @user that is in the controller. >> >> >> HTH, >> >> David >> >> >> > ?end >> >> >> > ?def do_get >> >> > ? ?put :show, {:id => "1"}, @session >> >> > ?end >> >> >> > ?login_and_before_filter_pass(:filter => :admin_only, >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) >> >> >> > ?it "should be successful" do >> >> > ? ?do_get >> >> > ? ?response.should be_success >> >> > ?end >> >> >> > ?it "should find the letter requested" do >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> >> > ? ?puts(@letter) >> >> > ? ?do_get >> >> > ?end >> >> >> > ?it "should assign the found letter for the view" do >> >> > ? ?# uncommenting will allow to pass >> >> > ? ?# @user.letters.should_receive(:find).with("1").and_return >> >> > (@letter) >> >> > ? ?do_get >> >> > ? ?assigns[:letter].should equal(@letter) >> >> > ?end >> >> >> > ?it "should render show template" do >> >> > ? ?do_get >> >> > ? ?response.should render_template("show") >> >> > ?end >> >> >> > end >> >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: >> >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: >> >> >> > How do I spec this following example from the Agile Rails Book listed >> >> >> > below. I am doing a similar thing in my controller and when I >> >> >> > attempted to change it to the collection way of doing the find I am >> >> >> > unable to get my spec to pass though I know it is working fine as my >> >> >> > cucumber features are passing >> >> >> >> > old rails way: >> >> >> >> > def show >> >> >> > @order = Order.find(params[:id]) >> >> >> > end >> >> >> >> > new rails way collection-based: >> >> >> >> > def show >> >> >> > id = params[:id] >> >> >> > @order = @user.orders.find(id) >> >> >> >> This code is inherently untestable in an isolated/granular way. Your >> >> >> options are: >> >> >> >> * write higher level specs that use real data >> >> >> ? * pros: simplicity and clarity in both code and specs >> >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow >> >> >> >> * write a very invasive spec with complex setup and instance_eval to >> >> >> set up the @user >> >> >> ? * pros: runs fast, no runtime dependency on correct models >> >> >> ? * cons: brittle due to dependency on internals, complex >> >> >> >> * refactor the code to make it easier to spec >> >> >> ? * pros: more highly decoupled code, simpler specs, fast >> >> >> ? * cons: more work up front, may disregard some of what Rails has to offer >> >> >> >> Note that the first two options are both brittle, but for different >> >> >> reasons. The first is brittle due to a runtime dependency. That means >> >> >> that when you run the spec the model has to be working correctly for >> >> >> the spec to pass, and a failure could be due to a problem in the model >> >> >> or in the controller. >> >> >> >> The second is due to a code dependency. That means that when you want >> >> >> to change this code, the spec will have to change as well. This is >> >> >> true of any case in which you use mocks or stubs to varying degrees, >> >> >> and that comes with its own tradeoffs. In this case, the necessary >> >> >> stubbing would be complex and invasive enough that it would be a >> >> >> concern to me. >> >> >> >> Getting to your original question - what does your spec look like now, >> >> >> and what failure message are you getting? >> >> >> >> Cheers, >> >> >> David >> >> >> >> > rescue >> >> >> > redirect_to :action => "index" >> >> >> > end >> >> >> > _______________________________________________ >> >> >> > rspec-users mailing list >> >> >> > rspec-us... at rubyforge.org >> >> >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> >> >> _______________________________________________ >> >> >> rspec-users mailing list >> >> >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users >> >> > _______________________________________________ >> >> > rspec-users mailing list >> >> > rspec-us... at rubyforge.org >> >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> >> _______________________________________________ >> >> rspec-users mailing list >> >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ >> > rspec-users mailing list >> > rspec-us... at rubyforge.org >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From celoserpa at gmail.com Wed Jul 22 12:19:24 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Wed, 22 Jul 2009 11:19:24 -0500 Subject: [rspec-users] Making sure a action exists (aka specing a controller action) Message-ID: <1e5bcefd0907220919j27d73791r711dd5741d31040c@mail.gmail.com> Hello list! In my efforts of following BDD in a disciplined manner, I found myself needing to spec a Rails action. The behavior I wanted to test was: "This controller should handle the index action", and since it is not good practice to go to the implementation before specing, I decided to spec the action too, so I could start defining it. I did: describe MyController do it "should handle get requests to the index action" do get :index response.should be_success end The problem is the assertion. Even if you don't have the action, the http response will be 200 OK. I tried to check for the "Unknown Action" text (with the have_text matcher) but strangely enough, even if you don't have the action defined in the controller, the response body is always empty (which doesn't happen if you request from mongrel, which shows an "Unknown action" page. Any ideas on how I could assert that the index action exists and that it fails when it is not there? Maybe this test doesn't make sense? Any suggestions welcome! Thanks, Marcelo. From amkirwan at gmail.com Wed Jul 22 12:38:49 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 09:38:49 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> Message-ID: <91a5e02f-e4f0-4746-a29c-d5cfeb7fadbc@z34g2000vbl.googlegroups.com> Here is the code, I pasted the letter_controller.rb, letters_controller_spec.rb and the controllers macros.rb, thanks again for your help. On Jul 22, 12:11?pm, David Chelimsky wrote: > On Wed, Jul 22, 2009 at 10:52 AM, amkirwan wrote: > > Okay I get the "and_return" part now thanks. > > > Here is the error > > >http://gist.github.com/152061 > > Well, the error seems quite clear. It's expecting a Letter and gets a Person. > > I know you've posted bits of the code in this thread, but would you > mind posting the entire spec and controller code in a gist? At least > the code related to the show action. > > Thanks, > David > > > > > > > > > On Jul 22, 11:39?am, David Chelimsky wrote: > >> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: > >> > Thanks for the help but I guess I am not getting something. How is > >> > @user= = mock_model(Person) and different then the following code: > > >> This is assigning the mock_model(Person) to a @user instance variable > >> in the spec. This is not the same user that is in the controller. > > >> > message = mock_model(Message) > >> > Message.stub!(:new).and_return message > >> > message.should_receive(:save) > >> > post :create > > >> This creates a mock_model(Message) and then tells Message to return it > >> when it receives :new, therefore message in the spec is the same > >> object as Message.new in the controller. > > >> > def create > >> > message = Message.new params[:new] > >> > message.save > >> > end > >> > I guess I don't understand why assigns[:letter] is expecting a Person > >> > instance instead of a Letter instance > > >> I'm not clear on what you mean by this. Would you please post the full > >> error message (either here or in a pastie or gist)? > > >> > On Jul 22, 10:42?am, David Chelimsky wrote: > >> >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > >> >> > My spec is a messed up because I have tried everything I can think of > >> >> > to mock but this is what I have for the show method. The @user > >> >> > instance is setup in the login_and_before_filter_pass macros with the > >> >> > following: @user = mock_model(Person, :null_object => true) > > >> >> > The error I keep receiving is that assigns[:letter].should equal > >> >> > @letter keeps return that it is expecting a Person object instead of a > >> >> > Letter object. The only way I can get it to pass is by putting > >> >> > @user.letters.should_receive(:find).with("1").and_return(@letter) > >> >> > directly in the "should assign the found letter for the view" > > >> >> > I feel like I must be missing something about how stubbing and mocking > >> >> > work > > >> >> > ?# Get /admin/letters/1 > >> >> > ?def show > >> >> > ? ?id = params[:id] > >> >> > ? ?@letter = ?@user.letters.find(id) > >> >> > ?end > > >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > >> >> > ?before(:each) do > >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > >> >> This @user is an instance variable in the spec, and is not the same > >> >> @user that is in the controller. > > >> >> HTH, > >> >> David > > >> >> > ?end > > >> >> > ?def do_get > >> >> > ? ?put :show, {:id => "1"}, @session > >> >> > ?end > > >> >> > ?login_and_before_filter_pass(:filter => :admin_only, > >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > >> >> > ?it "should be successful" do > >> >> > ? ?do_get > >> >> > ? ?response.should be_success > >> >> > ?end > > >> >> > ?it "should find the letter requested" do > >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > >> >> > ? ?puts(@letter) > >> >> > ? ?do_get > >> >> > ?end > > >> >> > ?it "should assign the found letter for the view" do > >> >> > ? ?# uncommenting will allow to pass > >> >> > ? ?# @user.letters.should_receive(:find).with("1").and_return > >> >> > (@letter) > >> >> > ? ?do_get > >> >> > ? ?assigns[:letter].should equal(@letter) > >> >> > ?end > > >> >> > ?it "should render show template" do > >> >> > ? ?do_get > >> >> > ? ?response.should render_template("show") > >> >> > ?end > > >> >> > end > > >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: > >> >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > >> >> >> > How do I spec this following example from the Agile Rails Book listed > >> >> >> > below. I am doing a similar thing in my controller and when I > >> >> >> > attempted to change it to the collection way of doing the find I am > >> >> >> > unable to get my spec to pass though I know it is working fine as my > >> >> >> > cucumber features are passing > > >> >> >> > old rails way: > > >> >> >> > def show > >> >> >> > @order = Order.find(params[:id]) > >> >> >> > end > > >> >> >> > new rails way collection-based: > > >> >> >> > def show > >> >> >> > id = params[:id] > >> >> >> > @order = @user.orders.find(id) > > >> >> >> This code is inherently untestable in an isolated/granular way. Your > >> >> >> options are: > > >> >> >> * write higher level specs that use real data > >> >> >> ? * pros: simplicity and clarity in both code and specs > >> >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow > > >> >> >> * write a very invasive spec with complex setup and instance_eval to > >> >> >> set up the @user > >> >> >> ? * pros: runs fast, no runtime dependency on correct models > >> >> >> ? * cons: brittle due to dependency on internals, complex > > >> >> >> * refactor the code to make it easier to spec > >> >> >> ? * pros: more highly decoupled code, simpler specs, fast > >> >> >> ? * cons: more work up front, may disregard some of what Rails has to offer > > >> >> >> Note that the first two options are both brittle, but for different > >> >> >> reasons. The first is brittle due to a runtime dependency. That means > >> >> >> that when you run the spec the model has to be working correctly for > >> >> >> the spec to pass, and a failure could be due to a problem in the model > >> >> >> or in the controller. > > >> >> >> The second is due to a code dependency. That means that when you want > >> >> >> to change this code, the spec will have to change as well. This is > >> >> >> true of any case in which you use mocks or stubs to varying degrees, > >> >> >> and that comes with its own tradeoffs. In this case, the necessary > >> >> >> stubbing would be complex and invasive enough that it would be a > >> >> >> concern to me. > > >> >> >> Getting to your original question - what does your spec look like now, > >> >> >> and what failure message are you getting? > > >> >> >> Cheers, > >> >> >> David > > >> >> >> > rescue > >> >> >> > redirect_to :action => "index" > >> >> >> > end > >> >> >> > _______________________________________________ > >> >> >> > rspec-users mailing list > >> >> >> > rspec-us... at rubyforge.org > >> >> >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> >> >> _______________________________________________ > >> >> >> rspec-users mailing list > >> >> >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > >> >> > _______________________________________________ > >> >> > rspec-users mailing list > >> >> > rspec-us... at rubyforge.org > >> >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> >> _______________________________________________ > >> >> rspec-users mailing list > >> >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > >> > _______________________________________________ > >> > rspec-users mailing list > >> > rspec-us... at rubyforge.org > >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Wed Jul 22 13:02:52 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 12:02:52 -0500 Subject: [rspec-users] collection-based finder methods In-Reply-To: <91a5e02f-e4f0-4746-a29c-d5cfeb7fadbc@z34g2000vbl.googlegroups.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> <91a5e02f-e4f0-4746-a29c-d5cfeb7fadbc@z34g2000vbl.googlegroups.com> Message-ID: <57c63afe0907221002q201bf00sb304da527f9d5581@mail.gmail.com> On Wed, Jul 22, 2009 at 11:38 AM, amkirwan wrote: > Here is the code, Where? I don't see code pasted or a link to a gist. > I pasted the letter_controller.rb, > letters_controller_spec.rb and the controllers macros.rb, thanks again > for your help. > > On Jul 22, 12:11?pm, David Chelimsky wrote: >> On Wed, Jul 22, 2009 at 10:52 AM, amkirwan wrote: >> > Okay I get the "and_return" part now thanks. >> >> > Here is the error >> >> >http://gist.github.com/152061 >> >> Well, the error seems quite clear. It's expecting a Letter and gets a Person. >> >> I know you've posted bits of the code in this thread, but would you >> mind posting the entire spec and controller code in a gist? At least >> the code related to the show action. >> >> Thanks, >> David >> >> >> >> >> >> >> >> > On Jul 22, 11:39?am, David Chelimsky wrote: >> >> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: >> >> > Thanks for the help but I guess I am not getting something. How is >> >> > @user= = mock_model(Person) and different then the following code: >> >> >> This is assigning the mock_model(Person) to a @user instance variable >> >> in the spec. This is not the same user that is in the controller. >> >> >> > message = mock_model(Message) >> >> > Message.stub!(:new).and_return message >> >> > message.should_receive(:save) >> >> > post :create >> >> >> This creates a mock_model(Message) and then tells Message to return it >> >> when it receives :new, therefore message in the spec is the same >> >> object as Message.new in the controller. >> >> >> > def create >> >> > message = Message.new params[:new] >> >> > message.save >> >> > end >> >> > I guess I don't understand why assigns[:letter] is expecting a Person >> >> > instance instead of a Letter instance >> >> >> I'm not clear on what you mean by this. Would you please post the full >> >> error message (either here or in a pastie or gist)? >> >> >> > On Jul 22, 10:42?am, David Chelimsky wrote: >> >> >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: >> >> >> > My spec is a messed up because I have tried everything I can think of >> >> >> > to mock but this is what I have for the show method. The @user >> >> >> > instance is setup in the login_and_before_filter_pass macros with the >> >> >> > following: @user = mock_model(Person, :null_object => true) >> >> >> >> > The error I keep receiving is that assigns[:letter].should equal >> >> >> > @letter keeps return that it is expecting a Person object instead of a >> >> >> > Letter object. The only way I can get it to pass is by putting >> >> >> > @user.letters.should_receive(:find).with("1").and_return(@letter) >> >> >> > directly in the "should assign the found letter for the view" >> >> >> >> > I feel like I must be missing something about how stubbing and mocking >> >> >> > work >> >> >> >> > ?# Get /admin/letters/1 >> >> >> > ?def show >> >> >> > ? ?id = params[:id] >> >> >> > ? ?@letter = ?@user.letters.find(id) >> >> >> > ?end >> >> >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do >> >> >> >> > ?before(:each) do >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> >> >> >> This @user is an instance variable in the spec, and is not the same >> >> >> @user that is in the controller. >> >> >> >> HTH, >> >> >> David >> >> >> >> > ?end >> >> >> >> > ?def do_get >> >> >> > ? ?put :show, {:id => "1"}, @session >> >> >> > ?end >> >> >> >> > ?login_and_before_filter_pass(:filter => :admin_only, >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) >> >> >> >> > ?it "should be successful" do >> >> >> > ? ?do_get >> >> >> > ? ?response.should be_success >> >> >> > ?end >> >> >> >> > ?it "should find the letter requested" do >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> >> >> > ? ?puts(@letter) >> >> >> > ? ?do_get >> >> >> > ?end >> >> >> >> > ?it "should assign the found letter for the view" do >> >> >> > ? ?# uncommenting will allow to pass >> >> >> > ? ?# @user.letters.should_receive(:find).with("1").and_return >> >> >> > (@letter) >> >> >> > ? ?do_get >> >> >> > ? ?assigns[:letter].should equal(@letter) >> >> >> > ?end >> >> >> >> > ?it "should render show template" do >> >> >> > ? ?do_get >> >> >> > ? ?response.should render_template("show") >> >> >> > ?end >> >> >> >> > end >> >> >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: >> >> >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: >> >> >> >> > How do I spec this following example from the Agile Rails Book listed >> >> >> >> > below. I am doing a similar thing in my controller and when I >> >> >> >> > attempted to change it to the collection way of doing the find I am >> >> >> >> > unable to get my spec to pass though I know it is working fine as my >> >> >> >> > cucumber features are passing >> >> >> >> >> > old rails way: >> >> >> >> >> > def show >> >> >> >> > @order = Order.find(params[:id]) >> >> >> >> > end >> >> >> >> >> > new rails way collection-based: >> >> >> >> >> > def show >> >> >> >> > id = params[:id] >> >> >> >> > @order = @user.orders.find(id) >> >> >> >> >> This code is inherently untestable in an isolated/granular way. Your >> >> >> >> options are: >> >> >> >> >> * write higher level specs that use real data >> >> >> >> ? * pros: simplicity and clarity in both code and specs >> >> >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow >> >> >> >> >> * write a very invasive spec with complex setup and instance_eval to >> >> >> >> set up the @user >> >> >> >> ? * pros: runs fast, no runtime dependency on correct models >> >> >> >> ? * cons: brittle due to dependency on internals, complex >> >> >> >> >> * refactor the code to make it easier to spec >> >> >> >> ? * pros: more highly decoupled code, simpler specs, fast >> >> >> >> ? * cons: more work up front, may disregard some of what Rails has to offer >> >> >> >> >> Note that the first two options are both brittle, but for different >> >> >> >> reasons. The first is brittle due to a runtime dependency. That means >> >> >> >> that when you run the spec the model has to be working correctly for >> >> >> >> the spec to pass, and a failure could be due to a problem in the model >> >> >> >> or in the controller. >> >> >> >> >> The second is due to a code dependency. That means that when you want >> >> >> >> to change this code, the spec will have to change as well. This is >> >> >> >> true of any case in which you use mocks or stubs to varying degrees, >> >> >> >> and that comes with its own tradeoffs. In this case, the necessary >> >> >> >> stubbing would be complex and invasive enough that it would be a >> >> >> >> concern to me. >> >> >> >> >> Getting to your original question - what does your spec look like now, >> >> >> >> and what failure message are you getting? >> >> >> >> >> Cheers, >> >> >> >> David >> >> >> >> >> > rescue >> >> >> >> > redirect_to :action => "index" >> >> >> >> > end From dchelimsky at gmail.com Wed Jul 22 13:23:38 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 12:23:38 -0500 Subject: [rspec-users] Making sure a action exists (aka specing a controller action) In-Reply-To: <1e5bcefd0907220919j27d73791r711dd5741d31040c@mail.gmail.com> References: <1e5bcefd0907220919j27d73791r711dd5741d31040c@mail.gmail.com> Message-ID: <57c63afe0907221023q30576e49rb045f5cd8b7eb203@mail.gmail.com> On Wed, Jul 22, 2009 at 11:19 AM, Marcelo de Moraes Serpa wrote: > Hello list! > > In my efforts of following BDD in a disciplined manner, I found myself > needing to spec a Rails action. > > The behavior I wanted to test was: "This controller should handle the > index action", and since it is not good practice to go to the > implementation before specing, I decided to spec the action too, so I > could start defining it. I did: > > describe MyController do > > ?it "should handle get requests to the index action" do > ? get :index > ? response.should be_success > ?end > > The problem is the assertion. Even if you don't have the action, the > http response will be 200 OK. I tried to check for the "Unknown > Action" text (with the have_text matcher) but strangely enough, even > if you don't have the action defined in the controller, the response > body is always empty (which doesn't happen if you request from > mongrel, which shows an "Unknown action" page. > > Any ideas on how I could assert that the index action exists and that > it fails when it is not there? Maybe this test doesn't make sense? Any > suggestions welcome! So it looks like you've uncovered a rspec-rails bug. I'm not sure when it appeared because I'm pretty sure there was a time when this would work as you expect, but right now there are no specs for missing actions - only missing templates - so something in rspec-rails is consuming the fact that the action is missing. I added https://rspec.lighthouseapp.com/projects/5645-rspec/tickets/860 - please feel free to follow and/or add comments to that ticket. Cheers, David > > Thanks, > > Marcelo. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From amkirwan at gmail.com Wed Jul 22 13:33:30 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 10:33:30 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <57c63afe0907221002q201bf00sb304da527f9d5581@mail.gmail.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> <91a5e02f-e4f0-4746-a29c-d5cfeb7fadbc@z34g2000vbl.googlegroups.com> <57c63afe0907221002q201bf00sb304da527f9d5581@mail.gmail.com> Message-ID: <1650369c-a546-42a6-98cb-40061fc1aa3a@d23g2000vbm.googlegroups.com> sorry here it is http://gist.github.com/152123 On Jul 22, 1:02?pm, David Chelimsky wrote: > On Wed, Jul 22, 2009 at 11:38 AM, amkirwan wrote: > > Here is the code, > > Where? I don't see code pasted or a link to a gist. > > > > > > > I pasted the letter_controller.rb, > > letters_controller_spec.rb and the controllers macros.rb, thanks again > > for your help. > > > On Jul 22, 12:11?pm, David Chelimsky wrote: > >> On Wed, Jul 22, 2009 at 10:52 AM, amkirwan wrote: > >> > Okay I get the "and_return" part now thanks. > > >> > Here is the error > > >> >http://gist.github.com/152061 > > >> Well, the error seems quite clear. It's expecting a Letter and gets a Person. > > >> I know you've posted bits of the code in this thread, but would you > >> mind posting the entire spec and controller code in a gist? At least > >> the code related to the show action. > > >> Thanks, > >> David > > >> > On Jul 22, 11:39?am, David Chelimsky wrote: > >> >> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: > >> >> > Thanks for the help but I guess I am not getting something. How is > >> >> > @user= = mock_model(Person) and different then the following code: > > >> >> This is assigning the mock_model(Person) to a @user instance variable > >> >> in the spec. This is not the same user that is in the controller. > > >> >> > message = mock_model(Message) > >> >> > Message.stub!(:new).and_return message > >> >> > message.should_receive(:save) > >> >> > post :create > > >> >> This creates a mock_model(Message) and then tells Message to return it > >> >> when it receives :new, therefore message in the spec is the same > >> >> object as Message.new in the controller. > > >> >> > def create > >> >> > message = Message.new params[:new] > >> >> > message.save > >> >> > end > >> >> > I guess I don't understand why assigns[:letter] is expecting a Person > >> >> > instance instead of a Letter instance > > >> >> I'm not clear on what you mean by this. Would you please post the full > >> >> error message (either here or in a pastie or gist)? > > >> >> > On Jul 22, 10:42?am, David Chelimsky wrote: > >> >> >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > >> >> >> > My spec is a messed up because I have tried everything I can think of > >> >> >> > to mock but this is what I have for the show method. The @user > >> >> >> > instance is setup in the login_and_before_filter_pass macros with the > >> >> >> > following: @user = mock_model(Person, :null_object => true) > > >> >> >> > The error I keep receiving is that assigns[:letter].should equal > >> >> >> > @letter keeps return that it is expecting a Person object instead of a > >> >> >> > Letter object. The only way I can get it to pass is by putting > >> >> >> > @user.letters.should_receive(:find).with("1").and_return(@letter) > >> >> >> > directly in the "should assign the found letter for the view" > > >> >> >> > I feel like I must be missing something about how stubbing and mocking > >> >> >> > work > > >> >> >> > ?# Get /admin/letters/1 > >> >> >> > ?def show > >> >> >> > ? ?id = params[:id] > >> >> >> > ? ?@letter = ?@user.letters.find(id) > >> >> >> > ?end > > >> >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > >> >> >> > ?before(:each) do > >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > >> >> >> This @user is an instance variable in the spec, and is not the same > >> >> >> @user that is in the controller. > > >> >> >> HTH, > >> >> >> David > > >> >> >> > ?end > > >> >> >> > ?def do_get > >> >> >> > ? ?put :show, {:id => "1"}, @session > >> >> >> > ?end > > >> >> >> > ?login_and_before_filter_pass(:filter => :admin_only, > >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > >> >> >> > ?it "should be successful" do > >> >> >> > ? ?do_get > >> >> >> > ? ?response.should be_success > >> >> >> > ?end > > >> >> >> > ?it "should find the letter requested" do > >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > >> >> >> > ? ?puts(@letter) > >> >> >> > ? ?do_get > >> >> >> > ?end > > >> >> >> > ?it "should assign the found letter for the view" do > >> >> >> > ? ?# uncommenting will allow to pass > >> >> >> > ? ?# @user.letters.should_receive(:find).with("1").and_return > >> >> >> > (@letter) > >> >> >> > ? ?do_get > >> >> >> > ? ?assigns[:letter].should equal(@letter) > >> >> >> > ?end > > >> >> >> > ?it "should render show template" do > >> >> >> > ? ?do_get > >> >> >> > ? ?response.should render_template("show") > >> >> >> > ?end > > >> >> >> > end > > >> >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: > >> >> >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > >> >> >> >> > How do I spec this following example from the Agile Rails Book listed > >> >> >> >> > below. I am doing a similar thing in my controller and when I > >> >> >> >> > attempted to change it to the collection way of doing the find I am > >> >> >> >> > unable to get my spec to pass though I know it is working fine as my > >> >> >> >> > cucumber features are passing > > >> >> >> >> > old rails way: > > >> >> >> >> > def show > >> >> >> >> > @order = Order.find(params[:id]) > >> >> >> >> > end > > >> >> >> >> > new rails way collection-based: > > >> >> >> >> > def show > >> >> >> >> > id = params[:id] > >> >> >> >> > @order = @user.orders.find(id) > > >> >> >> >> This code is inherently untestable in an isolated/granular way. Your > >> >> >> >> options are: > > >> >> >> >> * write higher level specs that use real data > >> >> >> >> ? * pros: simplicity and clarity in both code and specs > >> >> >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow > > >> >> >> >> * write a very invasive spec with complex setup and instance_eval to > >> >> >> >> set up the @user > >> >> >> >> ? * pros: runs fast, no runtime dependency on correct models > >> >> >> >> ? * cons: brittle due to dependency on internals, complex > > >> >> >> >> * refactor the code to make it easier to spec > >> >> >> >> ? * pros: more highly decoupled code, simpler specs, fast > >> >> >> >> ? * cons: more work up front, may disregard some of what Rails has to offer > > >> >> >> >> Note that the first two options are both brittle, but for different > >> >> >> >> reasons. The first is brittle due to a runtime dependency. That means > >> >> >> >> that when you run the spec the model has to be working correctly for > >> >> >> >> the spec to pass, and a failure could be due to a problem in the model > >> >> >> >> or in the controller. > > >> >> >> >> The second is due to a code dependency. That means that when you want > >> >> >> >> to change this code, the spec will have to change as well. This is > >> >> >> >> true of any case in which you use mocks or stubs to varying degrees, > >> >> >> >> and that comes with its own tradeoffs. In this case, the necessary > >> >> >> >> stubbing would be complex and invasive enough that it would be a > >> >> >> >> concern to me. > > >> >> >> >> Getting to your original question - what does your spec look like now, > >> >> >> >> and what failure message are you getting? > > >> >> >> >> Cheers, > >> >> >> >> David > > >> >> >> >> > rescue > >> >> >> >> > redirect_to :action => "index" > >> >> >> >> > end > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From amkirwan at gmail.com Wed Jul 22 14:23:41 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 11:23:41 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <1650369c-a546-42a6-98cb-40061fc1aa3a@d23g2000vbm.googlegroups.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <57c63afe0907220613p546d6f52y423bcc71b626367@mail.gmail.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> <91a5e02f-e4f0-4746-a29c-d5cfeb7fadbc@z34g2000vbl.googlegroups.com> <57c63afe0907221002q201bf00sb304da527f9d5581@mail.gmail.com> <1650369c-a546-42a6-98cb-40061fc1aa3a@d23g2000vbm.googlegroups.com> Message-ID: <10b51b5c-e9f6-43dc-bd83-6932fe731b6f@z28g2000vbl.googlegroups.com> I have also posted the original passing spec and controller before I tried to change the find to @user.letters.find(id) http://gist.github.com/152175 On Jul 22, 1:33?pm, amkirwan wrote: > sorry here it ishttp://gist.github.com/152123 > > On Jul 22, 1:02?pm, David Chelimsky wrote: > > > > > On Wed, Jul 22, 2009 at 11:38 AM, amkirwan wrote: > > > Here is the code, > > > Where? I don't see code pasted or a link to a gist. > > > > I pasted the letter_controller.rb, > > > letters_controller_spec.rb and the controllers macros.rb, thanks again > > > for your help. > > > > On Jul 22, 12:11?pm, David Chelimsky wrote: > > >> On Wed, Jul 22, 2009 at 10:52 AM, amkirwan wrote: > > >> > Okay I get the "and_return" part now thanks. > > > >> > Here is the error > > > >> >http://gist.github.com/152061 > > > >> Well, the error seems quite clear. It's expecting a Letter and gets a Person. > > > >> I know you've posted bits of the code in this thread, but would you > > >> mind posting the entire spec and controller code in a gist? At least > > >> the code related to the show action. > > > >> Thanks, > > >> David > > > >> > On Jul 22, 11:39?am, David Chelimsky wrote: > > >> >> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: > > >> >> > Thanks for the help but I guess I am not getting something. How is > > >> >> > @user= = mock_model(Person) and different then the following code: > > > >> >> This is assigning the mock_model(Person) to a @user instance variable > > >> >> in the spec. This is not the same user that is in the controller. > > > >> >> > message = mock_model(Message) > > >> >> > Message.stub!(:new).and_return message > > >> >> > message.should_receive(:save) > > >> >> > post :create > > > >> >> This creates a mock_model(Message) and then tells Message to return it > > >> >> when it receives :new, therefore message in the spec is the same > > >> >> object as Message.new in the controller. > > > >> >> > def create > > >> >> > message = Message.new params[:new] > > >> >> > message.save > > >> >> > end > > >> >> > I guess I don't understand why assigns[:letter] is expecting a Person > > >> >> > instance instead of a Letter instance > > > >> >> I'm not clear on what you mean by this. Would you please post the full > > >> >> error message (either here or in a pastie or gist)? > > > >> >> > On Jul 22, 10:42?am, David Chelimsky wrote: > > >> >> >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > > >> >> >> > My spec is a messed up because I have tried everything I can think of > > >> >> >> > to mock but this is what I have for the show method. The @user > > >> >> >> > instance is setup in the login_and_before_filter_pass macros with the > > >> >> >> > following: @user = mock_model(Person, :null_object => true) > > > >> >> >> > The error I keep receiving is that assigns[:letter].should equal > > >> >> >> > @letter keeps return that it is expecting a Person object instead of a > > >> >> >> > Letter object. The only way I can get it to pass is by putting > > >> >> >> > @user.letters.should_receive(:find).with("1").and_return(@letter) > > >> >> >> > directly in the "should assign the found letter for the view" > > > >> >> >> > I feel like I must be missing something about how stubbing and mocking > > >> >> >> > work > > > >> >> >> > ?# Get /admin/letters/1 > > >> >> >> > ?def show > > >> >> >> > ? ?id = params[:id] > > >> >> >> > ? ?@letter = ?@user.letters.find(id) > > >> >> >> > ?end > > > >> >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > > >> >> >> > ?before(:each) do > > >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > > >> >> >> This @user is an instance variable in the spec, and is not the same > > >> >> >> @user that is in the controller. > > > >> >> >> HTH, > > >> >> >> David > > > >> >> >> > ?end > > > >> >> >> > ?def do_get > > >> >> >> > ? ?put :show, {:id => "1"}, @session > > >> >> >> > ?end > > > >> >> >> > ?login_and_before_filter_pass(:filter => :admin_only, > > >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > > >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > > >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > > >> >> >> > ?it "should be successful" do > > >> >> >> > ? ?do_get > > >> >> >> > ? ?response.should be_success > > >> >> >> > ?end > > > >> >> >> > ?it "should find the letter requested" do > > >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > >> >> >> > ? ?puts(@letter) > > >> >> >> > ? ?do_get > > >> >> >> > ?end > > > >> >> >> > ?it "should assign the found letter for the view" do > > >> >> >> > ? ?# uncommenting will allow to pass > > >> >> >> > ? ?# @user.letters.should_receive(:find).with("1").and_return > > >> >> >> > (@letter) > > >> >> >> > ? ?do_get > > >> >> >> > ? ?assigns[:letter].should equal(@letter) > > >> >> >> > ?end > > > >> >> >> > ?it "should render show template" do > > >> >> >> > ? ?do_get > > >> >> >> > ? ?response.should render_template("show") > > >> >> >> > ?end > > > >> >> >> > end > > > >> >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: > > >> >> >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > > >> >> >> >> > How do I spec this following example from the Agile Rails Book listed > > >> >> >> >> > below. I am doing a similar thing in my controller and when I > > >> >> >> >> > attempted to change it to the collection way of doing the find I am > > >> >> >> >> > unable to get my spec to pass though I know it is working fine as my > > >> >> >> >> > cucumber features are passing > > > >> >> >> >> > old rails way: > > > >> >> >> >> > def show > > >> >> >> >> > @order = Order.find(params[:id]) > > >> >> >> >> > end > > > >> >> >> >> > new rails way collection-based: > > > >> >> >> >> > def show > > >> >> >> >> > id = params[:id] > > >> >> >> >> > @order = @user.orders.find(id) > > > >> >> >> >> This code is inherently untestable in an isolated/granular way. Your > > >> >> >> >> options are: > > > >> >> >> >> * write higher level specs that use real data > > >> >> >> >> ? * pros: simplicity and clarity in both code and specs > > >> >> >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow > > > >> >> >> >> * write a very invasive spec with complex setup and instance_eval to > > >> >> >> >> set up the @user > > >> >> >> >> ? * pros: runs fast, no runtime dependency on correct models > > >> >> >> >> ? * cons: brittle due to dependency on internals, complex > > > >> >> >> >> * refactor the code to make it easier to spec > > >> >> >> >> ? * pros: more highly decoupled code, simpler specs, fast > > >> >> >> >> ? * cons: more work up front, may disregard some of what Rails has to offer > > > >> >> >> >> Note that the first two options are both brittle, but for different > > >> >> >> >> reasons. The first is brittle due to a runtime dependency. That means > > >> >> >> >> that when you run the spec the model has to be working correctly for > > >> >> >> >> the spec to pass, and a failure could be due to a problem in the model > > >> >> >> >> or in the controller. > > > >> >> >> >> The second is due to a code dependency. That means that when you want > > >> >> >> >> to change this code, the spec will have to change as well. This is > > >> >> >> >> true of any case in which you use mocks or stubs to varying degrees, > > >> >> >> >> and that comes with its own tradeoffs. In this case, the necessary > > >> >> >> >> stubbing would be complex and invasive enough that it would be a > > >> >> >> >> concern to me. > > > >> >> >> >> Getting to your original question - what does your spec look like now, > > >> >> >> >> and what failure message are you getting? > > > >> >> >> >> Cheers, > > >> >> >> >> David > > > >> >> >> >> > rescue > > >> >> >> >> > redirect_to :action => "index" > > >> >> >> >> > end > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Wed Jul 22 14:33:00 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 13:33:00 -0500 Subject: [rspec-users] collection-based finder methods In-Reply-To: <1650369c-a546-42a6-98cb-40061fc1aa3a@d23g2000vbm.googlegroups.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> <91a5e02f-e4f0-4746-a29c-d5cfeb7fadbc@z34g2000vbl.googlegroups.com> <57c63afe0907221002q201bf00sb304da527f9d5581@mail.gmail.com> <1650369c-a546-42a6-98cb-40061fc1aa3a@d23g2000vbm.googlegroups.com> Message-ID: <57c63afe0907221133r287af09bx40f35a82d3ce6362@mail.gmail.com> On Wed, Jul 22, 2009 at 12:33 PM, amkirwan wrote: > sorry here it is > http://gist.github.com/152123 I see find_by_partners_uid referenced in the login_and_before_filter_pass macro and again in before(:each) - so there is likely some conflict there. Also, I don't see where find_by_partners_uid is ever called in the controller. > > On Jul 22, 1:02?pm, David Chelimsky wrote: >> On Wed, Jul 22, 2009 at 11:38 AM, amkirwan wrote: >> > Here is the code, >> >> Where? I don't see code pasted or a link to a gist. >> >> >> >> >> >> > I pasted the letter_controller.rb, >> > letters_controller_spec.rb and the controllers macros.rb, thanks again >> > for your help. >> >> > On Jul 22, 12:11?pm, David Chelimsky wrote: >> >> On Wed, Jul 22, 2009 at 10:52 AM, amkirwan wrote: >> >> > Okay I get the "and_return" part now thanks. >> >> >> > Here is the error >> >> >> >http://gist.github.com/152061 >> >> >> Well, the error seems quite clear. It's expecting a Letter and gets a Person. >> >> >> I know you've posted bits of the code in this thread, but would you >> >> mind posting the entire spec and controller code in a gist? At least >> >> the code related to the show action. >> >> >> Thanks, >> >> David >> >> >> > On Jul 22, 11:39?am, David Chelimsky wrote: >> >> >> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: >> >> >> > Thanks for the help but I guess I am not getting something. How is >> >> >> > @user= = mock_model(Person) and different then the following code: >> >> >> >> This is assigning the mock_model(Person) to a @user instance variable >> >> >> in the spec. This is not the same user that is in the controller. >> >> >> >> > message = mock_model(Message) >> >> >> > Message.stub!(:new).and_return message >> >> >> > message.should_receive(:save) >> >> >> > post :create >> >> >> >> This creates a mock_model(Message) and then tells Message to return it >> >> >> when it receives :new, therefore message in the spec is the same >> >> >> object as Message.new in the controller. >> >> >> >> > def create >> >> >> > message = Message.new params[:new] >> >> >> > message.save >> >> >> > end >> >> >> > I guess I don't understand why assigns[:letter] is expecting a Person >> >> >> > instance instead of a Letter instance >> >> >> >> I'm not clear on what you mean by this. Would you please post the full >> >> >> error message (either here or in a pastie or gist)? >> >> >> >> > On Jul 22, 10:42?am, David Chelimsky wrote: >> >> >> >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: >> >> >> >> > My spec is a messed up because I have tried everything I can think of >> >> >> >> > to mock but this is what I have for the show method. The @user >> >> >> >> > instance is setup in the login_and_before_filter_pass macros with the >> >> >> >> > following: @user = mock_model(Person, :null_object => true) >> >> >> >> >> > The error I keep receiving is that assigns[:letter].should equal >> >> >> >> > @letter keeps return that it is expecting a Person object instead of a >> >> >> >> > Letter object. The only way I can get it to pass is by putting >> >> >> >> > @user.letters.should_receive(:find).with("1").and_return(@letter) >> >> >> >> > directly in the "should assign the found letter for the view" >> >> >> >> >> > I feel like I must be missing something about how stubbing and mocking >> >> >> >> > work >> >> >> >> >> > ?# Get /admin/letters/1 >> >> >> >> > ?def show >> >> >> >> > ? ?id = params[:id] >> >> >> >> > ? ?@letter = ?@user.letters.find(id) >> >> >> >> > ?end >> >> >> >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do >> >> >> >> >> > ?before(:each) do >> >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> >> >> >> >> This @user is an instance variable in the spec, and is not the same >> >> >> >> @user that is in the controller. >> >> >> >> >> HTH, >> >> >> >> David >> >> >> >> >> > ?end >> >> >> >> >> > ?def do_get >> >> >> >> > ? ?put :show, {:id => "1"}, @session >> >> >> >> > ?end >> >> >> >> >> > ?login_and_before_filter_pass(:filter => :admin_only, >> >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, >> >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, >> >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) >> >> >> >> >> > ?it "should be successful" do >> >> >> >> > ? ?do_get >> >> >> >> > ? ?response.should be_success >> >> >> >> > ?end >> >> >> >> >> > ?it "should find the letter requested" do >> >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) >> >> >> >> > ? ?puts(@letter) >> >> >> >> > ? ?do_get >> >> >> >> > ?end >> >> >> >> >> > ?it "should assign the found letter for the view" do >> >> >> >> > ? ?# uncommenting will allow to pass >> >> >> >> > ? ?# @user.letters.should_receive(:find).with("1").and_return >> >> >> >> > (@letter) >> >> >> >> > ? ?do_get >> >> >> >> > ? ?assigns[:letter].should equal(@letter) >> >> >> >> > ?end >> >> >> >> >> > ?it "should render show template" do >> >> >> >> > ? ?do_get >> >> >> >> > ? ?response.should render_template("show") >> >> >> >> > ?end >> >> >> >> >> > end >> >> >> >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: >> >> >> >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: >> >> >> >> >> > How do I spec this following example from the Agile Rails Book listed >> >> >> >> >> > below. I am doing a similar thing in my controller and when I >> >> >> >> >> > attempted to change it to the collection way of doing the find I am >> >> >> >> >> > unable to get my spec to pass though I know it is working fine as my >> >> >> >> >> > cucumber features are passing >> >> >> >> >> >> > old rails way: >> >> >> >> >> >> > def show >> >> >> >> >> > @order = Order.find(params[:id]) >> >> >> >> >> > end >> >> >> >> >> >> > new rails way collection-based: >> >> >> >> >> >> > def show >> >> >> >> >> > id = params[:id] >> >> >> >> >> > @order = @user.orders.find(id) >> >> >> >> >> >> This code is inherently untestable in an isolated/granular way. Your >> >> >> >> >> options are: >> >> >> >> >> >> * write higher level specs that use real data >> >> >> >> >> ? * pros: simplicity and clarity in both code and specs >> >> >> >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow >> >> >> >> >> >> * write a very invasive spec with complex setup and instance_eval to >> >> >> >> >> set up the @user >> >> >> >> >> ? * pros: runs fast, no runtime dependency on correct models >> >> >> >> >> ? * cons: brittle due to dependency on internals, complex >> >> >> >> >> >> * refactor the code to make it easier to spec >> >> >> >> >> ? * pros: more highly decoupled code, simpler specs, fast >> >> >> >> >> ? * cons: more work up front, may disregard some of what Rails has to offer >> >> >> >> >> >> Note that the first two options are both brittle, but for different >> >> >> >> >> reasons. The first is brittle due to a runtime dependency. That means >> >> >> >> >> that when you run the spec the model has to be working correctly for >> >> >> >> >> the spec to pass, and a failure could be due to a problem in the model >> >> >> >> >> or in the controller. >> >> >> >> >> >> The second is due to a code dependency. That means that when you want >> >> >> >> >> to change this code, the spec will have to change as well. This is >> >> >> >> >> true of any case in which you use mocks or stubs to varying degrees, >> >> >> >> >> and that comes with its own tradeoffs. In this case, the necessary >> >> >> >> >> stubbing would be complex and invasive enough that it would be a >> >> >> >> >> concern to me. >> >> >> >> >> >> Getting to your original question - what does your spec look like now, >> >> >> >> >> and what failure message are you getting? >> >> >> >> >> >> Cheers, >> >> >> >> >> David >> >> >> >> >> >> > rescue >> >> >> >> >> > redirect_to :action => "index" >> >> >> >> >> > end >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From amkirwan at gmail.com Wed Jul 22 15:20:15 2009 From: amkirwan at gmail.com (amkirwan) Date: Wed, 22 Jul 2009 12:20:15 -0700 (PDT) Subject: [rspec-users] collection-based finder methods In-Reply-To: <57c63afe0907221133r287af09bx40f35a82d3ce6362@mail.gmail.com> References: <1b4cae23-0005-4d10-8233-a0b10c071941@b14g2000yqd.googlegroups.com> <02cf62a9-ce26-4922-8e08-585741e85355@t13g2000yqt.googlegroups.com> <57c63afe0907220742p54a02008yf42ff8ab3c987f47@mail.gmail.com> <57c63afe0907220839j74a7d9e7ob97622d8aee5bf03@mail.gmail.com> <57c63afe0907220911x2f687f8by2a046c0f42c809ad@mail.gmail.com> <91a5e02f-e4f0-4746-a29c-d5cfeb7fadbc@z34g2000vbl.googlegroups.com> <57c63afe0907221002q201bf00sb304da527f9d5581@mail.gmail.com> <1650369c-a546-42a6-98cb-40061fc1aa3a@d23g2000vbm.googlegroups.com> <57c63afe0907221133r287af09bx40f35a82d3ce6362@mail.gmail.com> Message-ID: the find_by_partners_uid is in the ApplicationController before_filter which handles setting the @user via RubyCAS I removed some of the duplication since posting. Just some left over code from when I was trying everything earlier to get this to pass. I thought it would be easy but didn't realize how difficult this step would be. I changed the following in macros to: def login_and_before_filter_pass(options) before(:each) do @user = mock_model(Person, :roles => true, :letters => true) @user.stub_chain(:roles, :find_by_name => true) Person.stub!(:find_by_partners_uid).once.and_return(@user) end That gets me closer I think but I receive a: should assign @user undefined method `find' for true:TrueClass which makes sense, just not sure where to go from here. On Jul 22, 2:33?pm, David Chelimsky wrote: > On Wed, Jul 22, 2009 at 12:33 PM, amkirwan wrote: > > sorry here it is > >http://gist.github.com/152123 > > I see find_by_partners_uid referenced in the > login_and_before_filter_pass macro and again in before(:each) - so > there is likely some conflict there. Also, I don't see where > find_by_partners_uid is ever called in the controller. > > > > > > > > > On Jul 22, 1:02?pm, David Chelimsky wrote: > >> On Wed, Jul 22, 2009 at 11:38 AM, amkirwan wrote: > >> > Here is the code, > > >> Where? I don't see code pasted or a link to a gist. > > >> > I pasted the letter_controller.rb, > >> > letters_controller_spec.rb and the controllers macros.rb, thanks again > >> > for your help. > > >> > On Jul 22, 12:11?pm, David Chelimsky wrote: > >> >> On Wed, Jul 22, 2009 at 10:52 AM, amkirwan wrote: > >> >> > Okay I get the "and_return" part now thanks. > > >> >> > Here is the error > > >> >> >http://gist.github.com/152061 > > >> >> Well, the error seems quite clear. It's expecting a Letter and gets a Person. > > >> >> I know you've posted bits of the code in this thread, but would you > >> >> mind posting the entire spec and controller code in a gist? At least > >> >> the code related to the show action. > > >> >> Thanks, > >> >> David > > >> >> > On Jul 22, 11:39?am, David Chelimsky wrote: > >> >> >> On Wed, Jul 22, 2009 at 10:31 AM, amkirwan wrote: > >> >> >> > Thanks for the help but I guess I am not getting something. How is > >> >> >> > @user= = mock_model(Person) and different then the following code: > > >> >> >> This is assigning the mock_model(Person) to a @user instance variable > >> >> >> in the spec. This is not the same user that is in the controller. > > >> >> >> > message = mock_model(Message) > >> >> >> > Message.stub!(:new).and_return message > >> >> >> > message.should_receive(:save) > >> >> >> > post :create > > >> >> >> This creates a mock_model(Message) and then tells Message to return it > >> >> >> when it receives :new, therefore message in the spec is the same > >> >> >> object as Message.new in the controller. > > >> >> >> > def create > >> >> >> > message = Message.new params[:new] > >> >> >> > message.save > >> >> >> > end > >> >> >> > I guess I don't understand why assigns[:letter] is expecting a Person > >> >> >> > instance instead of a Letter instance > > >> >> >> I'm not clear on what you mean by this. Would you please post the full > >> >> >> error message (either here or in a pastie or gist)? > > >> >> >> > On Jul 22, 10:42?am, David Chelimsky wrote: > >> >> >> >> On Wed, Jul 22, 2009 at 9:12 AM, amkirwan wrote: > >> >> >> >> > My spec is a messed up because I have tried everything I can think of > >> >> >> >> > to mock but this is what I have for the show method. The @user > >> >> >> >> > instance is setup in the login_and_before_filter_pass macros with the > >> >> >> >> > following: @user = mock_model(Person, :null_object => true) > > >> >> >> >> > The error I keep receiving is that assigns[:letter].should equal > >> >> >> >> > @letter keeps return that it is expecting a Person object instead of a > >> >> >> >> > Letter object. The only way I can get it to pass is by putting > >> >> >> >> > @user.letters.should_receive(:find).with("1").and_return(@letter) > >> >> >> >> > directly in the "should assign the found letter for the view" > > >> >> >> >> > I feel like I must be missing something about how stubbing and mocking > >> >> >> >> > work > > >> >> >> >> > ?# Get /admin/letters/1 > >> >> >> >> > ?def show > >> >> >> >> > ? ?id = params[:id] > >> >> >> >> > ? ?@letter = ?@user.letters.find(id) > >> >> >> >> > ?end > > >> >> >> >> > describe Admin::LettersController, "SHOW GET /admin/letters/1" do > > >> >> >> >> > ?before(:each) do > >> >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > > >> >> >> >> This @user is an instance variable in the spec, and is not the same > >> >> >> >> @user that is in the controller. > > >> >> >> >> HTH, > >> >> >> >> David > > >> >> >> >> > ?end > > >> >> >> >> > ?def do_get > >> >> >> >> > ? ?put :show, {:id => "1"}, @session > >> >> >> >> > ?end > > >> >> >> >> > ?login_and_before_filter_pass(:filter => :admin_only, > >> >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :request_method => :get, > >> >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :action => :show, > >> >> >> >> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? :parameters => {:cas_user => 'ak730'}) > > >> >> >> >> > ?it "should be successful" do > >> >> >> >> > ? ?do_get > >> >> >> >> > ? ?response.should be_success > >> >> >> >> > ?end > > >> >> >> >> > ?it "should find the letter requested" do > >> >> >> >> > ? ?@user.letters.should_receive(:find).with("1").and_return(@letter) > >> >> >> >> > ? ?puts(@letter) > >> >> >> >> > ? ?do_get > >> >> >> >> > ?end > > >> >> >> >> > ?it "should assign the found letter for the view" do > >> >> >> >> > ? ?# uncommenting will allow to pass > >> >> >> >> > ? ?# @user.letters.should_receive(:find).with("1").and_return > >> >> >> >> > (@letter) > >> >> >> >> > ? ?do_get > >> >> >> >> > ? ?assigns[:letter].should equal(@letter) > >> >> >> >> > ?end > > >> >> >> >> > ?it "should render show template" do > >> >> >> >> > ? ?do_get > >> >> >> >> > ? ?response.should render_template("show") > >> >> >> >> > ?end > > >> >> >> >> > end > > >> >> >> >> > On Jul 22, 9:13?am, David Chelimsky wrote: > >> >> >> >> >> On Tue, Jul 21, 2009 at 11:21 PM, amkirwan wrote: > >> >> >> >> >> > How do I spec this following example from the Agile Rails Book listed > >> >> >> >> >> > below. I am doing a similar thing in my controller and when I > >> >> >> >> >> > attempted to change it to the collection way of doing the find I am > >> >> >> >> >> > unable to get my spec to pass though I know it is working fine as my > >> >> >> >> >> > cucumber features are passing > > >> >> >> >> >> > old rails way: > > >> >> >> >> >> > def show > >> >> >> >> >> > @order = Order.find(params[:id]) > >> >> >> >> >> > end > > >> >> >> >> >> > new rails way collection-based: > > >> >> >> >> >> > def show > >> >> >> >> >> > id = params[:id] > >> >> >> >> >> > @order = @user.orders.find(id) > > >> >> >> >> >> This code is inherently untestable in an isolated/granular way. Your > >> >> >> >> >> options are: > > >> >> >> >> >> * write higher level specs that use real data > >> >> >> >> >> ? * pros: simplicity and clarity in both code and specs > >> >> >> >> >> ? * cons: brittle due to runtime dependency on correct models, runs slow > > >> >> >> >> >> * write a very invasive spec with complex setup and instance_eval to > >> >> >> >> >> set up the @user > >> >> >> >> >> ? * pros: runs fast, no runtime dependency on correct models > >> >> >> >> >> ? * cons: brittle due to dependency on internals, complex > > >> >> >> >> >> * refactor the code to make it easier to spec > >> >> >> >> >> ? * pros: more highly decoupled code, simpler specs, fast > >> >> >> >> >> ? * cons: more work up front, may disregard some of what Rails has to offer > > >> >> >> >> >> Note that the first two options are both brittle, but for different > >> >> >> >> >> reasons. The first is brittle due to a runtime dependency. That means > >> >> >> >> >> that when you run the spec the model has to be working correctly for > >> >> >> >> >> the spec to pass, and a failure could be due to a problem in the model > >> >> >> >> >> or in the controller. > > >> >> >> >> >> The second is due to a code dependency. That means that when you want > >> >> >> >> >> to change this code, the spec will have to change as well. This is > >> >> >> >> >> true of any case in which you use mocks or stubs to varying degrees, > >> >> >> >> >> and that comes with its own tradeoffs. In this case, the necessary > >> >> >> >> >> stubbing would be complex and invasive enough that it would be a > >> >> >> >> >> concern to me. > > >> >> >> >> >> Getting to your original question - what does your spec look like now, > >> >> >> >> >> and what failure message are you getting? > > >> >> >> >> >> Cheers, > >> >> >> >> >> David > > >> >> >> >> >> > rescue > >> >> >> >> >> > redirect_to :action => "index" > >> >> >> >> >> > end > > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Wed Jul 22 17:36:49 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 22 Jul 2009 16:36:49 -0500 Subject: [rspec-users] Unsubscribing from the list ... In-Reply-To: <29a0119e0907221413o33eab2c8s57cb20783fb07e77@mail.gmail.com> References: <29a0119e0907221413o33eab2c8s57cb20783fb07e77@mail.gmail.com> Message-ID: <57c63afe0907221436gd1c34abqc8b3dfd8e4fce76f@mail.gmail.com> On Wed, Jul 22, 2009 at 4:13 PM, Bryan Ray wrote: > I'm currently subscribed to this list on two email addresses (personal and > 'work') ... i would like to remove this list from my 'personal' just to > reduce the amount of email ... > I have tried using the unsubscribe feature on the web site When you say "the website" do you mean this: http://rubyforge.org/mailman/listinfo/rspec-users ? > as well as > attempting to send an email with "unsubscribe" but it refuses to get me off > this list. > Is there a secret that I'm missing? How do I get off this crazy ride? > > -- > Bryan Ray > http://www.bryanray.net > > "Programming today is a race between software engineers striving to build > bigger and better idiot-proof programs, and the Universe trying to produce > bigger and better idiots. So far, the Universe is winning." > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From nathan.benes at gmail.com Thu Jul 23 12:13:50 2009 From: nathan.benes at gmail.com (nbenes) Date: Thu, 23 Jul 2009 09:13:50 -0700 (PDT) Subject: [rspec-users] any idea why spec_server gets 'cannot remove Object' error? In-Reply-To: References: <7df13a180210246a40d87100c25a9e7d@ruby-forum.com> <57c63afe0906230719t211872abm67200c5aa1d22d5c@mail.gmail.com> Message-ID: <24629257.post@talk.nabble.com> Tim Harper wrote: > >> Have you heard of anyone that has rspec/spork/cucumber etc running under >> cygwin, colinux, andLinux, or Ulteo Virtual Desktop. Do any of them >> allow for fork? > > yes, yes, yes, don't know > > cygwin is slow, I would avoid it. andLinux is a ubuntu-KDE > distribution that uses colinux. I use andLinux at home, and it's > simply awesome. > > Tim > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > Hey Tim, I was trying to get Spork to work on Cygwin (Yes...Cygwin is slow, hence the desire to get everything going on a DRb). I installed the Spork gem and setup the files for spec/cucumber just like it said to. However whenever I try to use it with either one it doesn't work. Spork starts up just fine, but then when I run: script/spec spec --drb It fails out on me. I also tried doing it with autospec (and the proper changes to the spec_helper file), but same result. $ spork Using RSpec Preloading Rails environment Loading Spork.prefork block... Spork is ready and listening on 8989! 3 [main] ruby 5696 C:\cygwin\bin\ruby.exe: *** fatal error - unable to remap C:\cygwin\bin\cyggcrypt-11.dll to same address as parent(0x5390000) != 0x54A0000 15 [main] ruby 3344 child_info::sync: wait failed, pid 5696, Win32 error 183 204 [main] ruby 3344 fork: child 5696 - died waiting for dll loading, errno 11 That's the error I get. The 3 [main] ruby etc. line shows up when I attempt to run my specs through it in another terminal. The next two lines (15 and 204) show up about 5 minutes later if letting it idle. Any idea on what it's doing or how I can solve this? I have rspec 1.2.8 ruby 1.8.7 rails 2.3.2 Supposedly cygwin will support the forking so something tells me this is another issue entirely. Any ideas? -- View this message in context: http://www.nabble.com/any-idea-why-spec_server-gets-%27cannot-remove-Object%27-error--tp24162534p24629257.html Sent from the rspec-users mailing list archive at Nabble.com. From chris at silhouettesolutions.net Thu Jul 23 17:26:32 2009 From: chris at silhouettesolutions.net (Chris Sund) Date: Thu, 23 Jul 2009 14:26:32 -0700 (PDT) Subject: [rspec-users] What else should I include in this model spec? Message-ID: <7801c655-b3c6-4307-b18d-c4714f9e798c@13g2000prl.googlegroups.com> Hey everyone, I just created a spec for an existing model I already developed prior to reading the rspec book. I have 5 examples and they are all passing. I'm just not sure what else I need to add or how I implement it. In this model I have some methods for adding existing names to an account, and also geocoding addresses for my google map. Here's my model code....followed by my current spec file. In my spec file I basically added my validation information, I'm just not sure how to add the other stuff. Any suggestions are appreciated. Keep in mind I'm trying to get my current app up to date with testing procedures. I wrote a portion of the app prior to having any knowledge of BDD and now I'm playing catch up on the testing side of things. In every case I'm also getting familiar with the ruby syntax - if you see something seriously wrong let me know. Many Thanks! - this group is great. MODEL ACCOUNT.RB------------ class Account < ActiveRecord::Base has_many :names, :dependent => :destroy has_many :readings, :dependent => :destroy validates_presence_of :s_property_no, :s_service_address, :s_service_address_zip # On the fly Geocoding acts_as_mappable before_validation_on_create :geocode_address before_validation_on_update :geocode_address # Thinking Sphinx Search Indexes define_index do indexes s_property_no, :sortable => true indexes s_service_address, :facet => true indexes names.name, :as => :property_name end # adding human attributes to error messages HUMAN_ATTRIBUTES = { :s_property_no => "Property Number" } def self.human_attribute_name(attr) HUMAN_ATTRIBUTES[attr.to_sym] || super end # end human attributes def new_name_attributes=(name_attributes) name_attributes.each do |attributes| names.build(attributes) end end after_update :save_names def existing_name_attributes=(name_attributes) names.reject(&:new_record?).each do |name| attributes = name_attributes[name.id.to_s] if attributes name.attributes = attributes else names.delete(name) end end end def save_names names.each do |name| name.save(false) end end # Map Information for Geocoding def full_address "#{s_service_address},#{s_service_address_zip}" end private def geocode_address if s_service_address_changed? or s_service_address_zip_changed? geo = GeoKit::Geocoders::MultiGeocoder.geocode(full_address) errors.add(:address, "Could not geocode address" ) unless geo.success self.s_latitude, self.s_longitude = geo.lat, geo.lng if geo.success end end end CURRENT SPEC FILE - ACCOUNT_SPEC.RB ---------------------------- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper' ) describe Account do describe "validations" do before(:each) do @account= Account.new(:s_property_no =>"1000", :s_service_address => "1201 Washington St", :s_service_address_zip => "59601" ) end it "should be valid with valid attributes (property no, service address and zip)" do @account.should be_valid end it "should not be valid without a service address or zipcode" do @account.s_service_address = nil @account.s_service_address_zip = nil @account.s_property_no = nil @account.should_not be_valid end it "should have one error on service address" do @account.s_service_address = nil @account.should have(1).error_on(:s_service_address) end it "should have one error on property number" do @account.s_property_no = nil @account.should have(1).error_on(:s_property_no) end it "should have one error on service address zip" do @account.s_service_address_zip = nil @account.should have(1).error_on(:s_service_address_zip) end end end From chris at silhouettesolutions.net Thu Jul 23 17:58:07 2009 From: chris at silhouettesolutions.net (Chris Sund) Date: Thu, 23 Jul 2009 14:58:07 -0700 (PDT) Subject: [rspec-users] Rspec book review... Message-ID: <80cd3575-b06e-42ee-ade6-194a0860292e@k30g2000yqf.googlegroups.com> Hey everyone, I just thought I would provide some feedback on my experience with the rspec book - I "just" finished it. I'll explain my background a little bit so you can see how I was influenced and how well I feel I absorbed the information. My background is in desktop application development using Visual Foxpro. I decided about 6 months "or more" ago I wanted to abandon Visual Foxpro and start web application development. In my case I have had "no" experience with TDD or BDD or any type of Test Unit procedures. As I started down this road I bought just about every Rails book I could find, and read most of them. Each book kept re-iterating the fact I needed to write tests. Due to the fact I was new to the Ruby language I put it off for a while (learning ruby alone seemed like enough to bite off at the point) and like I said, BDD or TDD was never a part of my development process in the past. I finally took the plunge and Ben Mabey suggested I read the Rspec book to get started. Now that I finished the book I have to say it was excellent. Although I don't feel competent to self generate the proper syntaxing in my spec files, I do understand how they work, and how to implement them. In addition to the spec files I think I finally understand how to use this with Cucumber. I feel fortunate that I started down this path using rspec and cucumber rather than the built in rails test unit and fixture stuff. I feel the book helped get me started down the right path and it was definitely well written. For a noob like me, I think it was an excellent place to start, and it took me from zero knowledge to a firm knowledge of the process. My only problem now and probably for the next year is remembering how to implement the proper syntax for something or where to find it on the web. In regard to things I "wish" had been covered more - I only wish one other item was covered a little more in depth. The use of Test Data builders like Fixjour, Factory Girl, or Object Daddy in conjunction with Rspec would have been a nice chapter. I'm sure once I want to do this in my app I will figure out how these tools work. In any case, the book was great, I want to thank everyone that wrote it, and a special thanks to Ben Mabey for pointing me to the book. Now if everyone can deal with all of the questions I'm going to have in regard to proper syntaxing I will be twice as thankful! Chris From celoserpa at gmail.com Thu Jul 23 19:39:26 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Thu, 23 Jul 2009 18:39:26 -0500 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec Message-ID: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> Hello list, >From what I could see, the lines between mocks and stubs are subtle, but the general idea I got is that mocks set expectations and stubs are only dummy objects/method calls. What confused me though, is that the stub() method is an alias for mock() in Spec::Mocks::ExampleMethods. So a stub is a mock? Also, another thing that is confusing: You have stub and stub! methods in the Spec::Mocks::Methods namespace, what is the difference between Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ? Thanks in advance, Marcelo. From dchelimsky at gmail.com Thu Jul 23 20:33:12 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 23 Jul 2009 19:33:12 -0500 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> Message-ID: <57c63afe0907231733g24ec75b3w83156245c011fec8@mail.gmail.com> On Thu, Jul 23, 2009 at 6:39 PM, Marcelo de Moraes Serpa wrote: > Hello list, Oi Marcelo, > > >From what I could see, the lines between mocks and stubs are subtle, > but the general idea I got is that mocks set expectations and stubs > are only dummy objects/method calls. What confused me though, is that > the stub() method is an alias for mock() in > Spec::Mocks::ExampleMethods. So a stub is a mock? The conversation about stubs and mocks are usually about Mock Objects and Test Stubs - i.e. two theoretically different kinds of objects. The functional difference between them, however, is really at the _method_ level, and even then, they are mostly the same: Both Method Stubs (stub()) and Method Expectations (should_receive()): * respond to a call * can return a canned value * can return different values to subsequent calls * can raise errors * can yield values to a block * etc, etc, etc Only Method Expectations can verify that they have been received. That is the primary difference between them. Keep in mind that these ideas evolved and appeared at different times and as different things, but over time have evolved to nearly the same thing with the exception of verification. The primary benefit we get from verification is when we care about a specific interaction that causes a side effect that we can not perceive from the object we're dealing with. Let's say we want to spec that an object logs something in response to a message given specific conditions: describe Account do it "logs a message when it is queried for its balance" do logger = mock('logger') logger.should_receive(:log).with(/queried for balance on/) account = Account.new(logger) account.balance end end Note that this spec never expects any values on the account itself. This is the classic example of the use of a mock object, or more accurately (in my opinion) a message expectation. Now in cases like we see so often in Rails controller specs, in my view there is little difference between these two: describe ThingController do describe "GET index" do it "assigns all things as @things (using a message expectation)" do thing = stub_model(Thing) Thing.should_receive(:all).and_return([thing]) get :index assigns[:things].should == [thing] end it "assigns all things as @things (using a stub)" do thing = stub_model(Thing) Thing.stub(:all).and_return([thing]) get :index assigns[:things].should == [thing] end end end In this case, the implicit verification in the first example doesn't buy us all that much, so the second example does a perfectly good job and is slightly less brittle. > Also, another thing that is confusing: You have stub and stub! methods > in the Spec::Mocks::Methods namespace, what is the difference between > Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ? The original method was stub!, which I will confess to introducing, and for the wrong reasons, due to my misunderstanding of the meaning of ! in ruby methods. I recently added stub (with no !) with no particular fanfare, as I don't intend to remove stub! (at least not any time soon). Right now the docs don't make it clear, so I need to fix that - but I'd say that stub() is the method to use and stub!() is there for backwards compatibility. Cheers, David ps - seu nome pare?e brasileiro - voc? ?? > > Thanks in advance, > > Marcelo. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Thu Jul 23 20:41:18 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 23 Jul 2009 19:41:18 -0500 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: <57c63afe0907231733g24ec75b3w83156245c011fec8@mail.gmail.com> References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> <57c63afe0907231733g24ec75b3w83156245c011fec8@mail.gmail.com> Message-ID: <57c63afe0907231741q1dbb02c5na4e08df7d4754a35@mail.gmail.com> On Thu, Jul 23, 2009 at 7:33 PM, David Chelimsky wrote: > On Thu, Jul 23, 2009 at 6:39 PM, Marcelo de Moraes > Serpa wrote: >> Hello list, > > Oi Marcelo, > >> >> >From what I could see, the lines between mocks and stubs are subtle, >> but the general idea I got is that mocks set expectations and stubs >> are only dummy objects/method calls. What confused me though, is that >> the stub() method is an alias for mock() in >> Spec::Mocks::ExampleMethods. So a stub is a mock? > > The conversation about stubs and mocks are usually about Mock Objects > and Test Stubs - i.e. two theoretically different kinds of objects. > The functional difference between them, however, is really at the > _method_ level, and even then, they are mostly the same: > > Both Method Stubs (stub()) and Method Expectations (should_receive()): > * respond to a call > * can return a canned value > * can return different values to subsequent calls > * can raise errors > * can yield values to a block > * etc, etc, etc > > Only Method Expectations can verify that they have been received. That > is the primary difference between them. > > Keep in mind that these ideas evolved and appeared at different times > and as different things, but over time have evolved to nearly the same > thing with the exception of verification. > > The primary benefit we get from verification is when we care about a > specific interaction that causes a side effect that we can not > perceive from the object we're dealing with. Let's say we want to spec > that an object logs something in response to a message given specific > conditions: > > describe Account do > ?it "logs a message when it is queried for its balance" do > ? ?logger = mock('logger') > ? ?logger.should_receive(:log).with(/queried for balance on/) > ? ?account = Account.new(logger) > ? ?account.balance > ?end > end > > Note that this spec never expects any values on the account itself. > This is the classic example of the use of a mock object, or more > accurately (in my opinion) a message expectation. > > Now in cases like we see so often in Rails controller specs, in my > view there is little difference between these two: > > describe ThingController do > ?describe "GET index" do > ? ?it "assigns all things as @things (using a message expectation)" do > ? ? ?thing = stub_model(Thing) > ? ? ?Thing.should_receive(:all).and_return([thing]) > ? ? ?get :index > ? ? ?assigns[:things].should == [thing] > ? ?end > > ? ?it "assigns all things as @things (using a stub)" do > ? ? ?thing = stub_model(Thing) > ? ? ?Thing.stub(:all).and_return([thing]) > ? ? ?get :index > ? ? ?assigns[:things].should == [thing] > ? ?end > ?end > end > > In this case, the implicit verification in the first example doesn't > buy us all that much, so the second example does a perfectly good job > and is slightly less brittle. > >> Also, another thing that is confusing: You have stub and stub! methods >> in the Spec::Mocks::Methods namespace, what is the difference between >> Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ? > > The original method was stub!, which I will confess to introducing, > and for the wrong reasons, due to my misunderstanding of the meaning > of ! in ruby methods. > > I recently added stub (with no !) with no particular fanfare, as I > don't intend to remove stub! (at least not any time soon). Right now > the docs don't make it clear, so I need to fix that - but I'd say that > stub() is the method to use and stub!() is there for backwards > compatibility. I misread your question :) Here's the answer you're looking for: the diff between Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub: Spec::Mocks::ExampleMethods#stub creates an instance of Spec::Mocks::Mock :) Oh the irony. So does Spec::Mocks::ExampleMethods#mock. Spec::Mocks::Methods#stub (and Spec::Mocks::Methods#stub!) creates a method stub on self. HTH, David > > Cheers, > David > > ps - seu nome pare?e brasileiro - voc? ?? > >> >> Thanks in advance, >> >> Marcelo. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > From ben at benmabey.com Thu Jul 23 20:41:43 2009 From: ben at benmabey.com (Ben Mabey) Date: Thu, 23 Jul 2009 18:41:43 -0600 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> Message-ID: <4A690347.7040609@benmabey.com> Marcelo de Moraes Serpa wrote: > Hello list, > > >From what I could see, the lines between mocks and stubs are subtle, > but the general idea I got is that mocks set expectations and stubs > are only dummy objects/method calls. What confused me though, is that > the stub() method is an alias for mock() in > Spec::Mocks::ExampleMethods. So a stub is a mock? > Yes, no, and maybe so. :p I agree with your general understanding that "mocks set expectations and stubs are only dummy objects/method calls". The fact that rspec using the same machinery to provide stubs and mocks is really inconsequential because the deciding factor of what they are is how you use them. You can, IMO, use a "mock" like a stub and a "stub" like a mock.. Take this for example: describe "#some_method" do it "delegates to some_obejct" do some_object = stub('some object', :some_method => "foo") my_object = MyObject.new(some_object) my_object.some_method.should == "foo" end end We are using a stub as a dummy object, and yet our expectation is clearing testing the method call. So is the above a stub or is it really a mock? I would say that it is acting like a mock. I hope that others on the list will correct me if I am wrong making this observation/conclusion. Likewise we could use a mock as a dummy object and never set any expectations on it and it would be acting like a stub. Even though stub and mock is the same in RSpec you should still use the correct name when using them. > Also, another thing that is confusing: You have stub and stub! methods > in the Spec::Mocks::Methods namespace, what is the difference between > Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ? > Spec::Mocks::ExampleMethods are the methods that you can call during the course of a code example. So when you say stub("something") the method in Spec::Mocks::ExampleMethods gets called and returns you a stub. Now, that stub object now has it's own #stub method which lives on Spec::Mocks::Methods#stub. That call will stub out the provided method and return value... So my_stub.stub(:foo, "bar"), however that is aliased from #stub! which is used most often. This is all from memory so I could be wrong but that is the general gist of it. HTH, -Ben From nick at deadorange.com Thu Jul 23 21:29:02 2009 From: nick at deadorange.com (Nick Hoffman) Date: Thu, 23 Jul 2009 21:29:02 -0400 Subject: [rspec-users] Rspec book review... In-Reply-To: <80cd3575-b06e-42ee-ade6-194a0860292e@k30g2000yqf.googlegroups.com> References: <80cd3575-b06e-42ee-ade6-194a0860292e@k30g2000yqf.googlegroups.com> Message-ID: <16827fa20907231829s570c258bpd7c43b4c90606ec@mail.gmail.com> On Thu, Jul 23, 2009 at 5:58 PM, Chris Sund wrote: > Hey everyone, > > I just thought I would provide some feedback on my experience with the > rspec book - I "just" finished it. ..snip.. > In any case, the book was great, I want to thank everyone that wrote > it, and a special thanks to Ben Mabey for pointing me to the book. Now > if everyone can deal with all of the questions I'm going to have in > regard to proper syntaxing I will be twice as thankful! > > > Chris G'day Chris. It sounds like you're well on your way towards going whole hog with BDD. Just keep at it, and it'll feel like second nature before you know it. You have all of the necessary resources at hand, and you've done lots of research. Now you just need to become experienced with BDD, RSpec, and Cucumber. The only way to do that is to write lots of code, so have fun! -Nick From dchelimsky at gmail.com Thu Jul 23 21:47:36 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 23 Jul 2009 18:47:36 -0700 (PDT) Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: <4A690347.7040609@benmabey.com> References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> <4A690347.7040609@benmabey.com> Message-ID: On Jul 23, 7:41?pm, Ben Mabey wrote: > Marcelo de Moraes Serpa wrote:> Hello list, > > > >From what I could see, the lines between mocks and stubs are subtle, > > but the general idea I got is that mocks set expectations and stubs > > are only dummy objects/method calls. What confused me though, is that > > the stub() method is an alias for mock() in > > Spec::Mocks::ExampleMethods. So a stub is a mock? > > Yes, no, and maybe so. :p ?I agree with your general understanding that > "mocks set expectations and stubs are only dummy objects/method calls". ? > The fact that rspec using the same machinery to provide stubs and mocks > is really inconsequential because the deciding factor of what they are > is how you use them. ?You can, IMO, use a "mock" like a stub and a > "stub" like a mock.. Take this for example: > > describe "#some_method" do > ? it "delegates to some_obejct" do > ? ? some_object = stub('some object', :some_method => "foo") > ? ? my_object = MyObject.new(some_object) > > ? ? my_object.some_method.should == "foo" > ? end > end > > We are using a stub as a dummy object, and yet our expectation is > clearing testing the method call. ?So is the above a stub or is it > really a mock? ?I would say that it is acting like a mock. ?I hope that > others on the list will correct me if I am wrong making this > observation/conclusion. ? Likewise we could use a mock as a dummy object > and never set any expectations on it and it would be acting like a > stub. ?Even though stub and mock is the same in RSpec you should still > use the correct name when using them. > > > Also, another thing that is confusing: You have stub and stub! methods > > in the Spec::Mocks::Methods namespace, what is the difference between > > Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ? > > Spec::Mocks::ExampleMethods are the methods that you can call during the course of a code example. ?So when you say stub("something") the method in Spec::Mocks::ExampleMethods gets called and returns you a stub. ?Now, that stub object now has it's own #stub method which lives on Spec::Mocks::Methods#stub. ?That call will stub out the provided method and return value... So my_stub.stub(:foo, "bar"), however that is aliased from #stub! which is used most often. ?This is all from memory so I could be wrong but that is the general gist of it. > > HTH, > -Ben Ben and I are secretly the same person. From lists at ruby-forum.com Fri Jul 24 00:59:49 2009 From: lists at ruby-forum.com (Nathan Benes) Date: Fri, 24 Jul 2009 06:59:49 +0200 Subject: [rspec-users] How detailed should error scenarios be? Message-ID: <7f3d2246ab54ad81996b502fc8d4a348@ruby-forum.com> I'm fairly new to cucumber and rspec but so far am falling in love with both. I've read up on several different articles concerning these testing tools including the 'beta' BDD rspec/cucumber book. I saw this thread here: http://www.ruby-forum.com/topic/183428 Which concerns how small or detailed specs and scenarios should be - and the basic gist was to keep scenarios at a higher level and leave the detailed form fields etc to rspec specs. This was all wonderfully insightful but it brings me to another question. How detailed (or should they be included in cucumber tests at all?) should the error path be? "Happy paths" are great to test, but it's also necessary to test error paths so that users aren't encountering rails stack traces, empty feedback, etc. Should there be one scenario per "empty" field? Should there be one scenario per validation check per field? Should it be condensed to a single scenario that attempts to encompass all error paths? I have one specific, seemingly overly complicated scenario that attempts to go the one scenario per validation check per field route: Scenario Outline: Add account with invalid fields Given I am logged in as BudgetTest And I am on the New Account page When I fill in the form for account "" with valid data And I submit the data "" for the "" field, "" And I submit the data "" for the "" field, "" And I press "Add Account" Then I should see the rendered template for "the new account page" And I should see an error indicating "" was "" I've removed the Scenarios: blocks because they would wordwrap and look terrible/undreadable. Following this is two sets of scenarios: Scenarios: missing required fields Scenarios: submitting bad data Some of the fields compare data with each other to determine validity which is why there's two data entries in the scenario outline. If the second is left blank then the defaults that were set in "When I fill in the form..." are used for it. Each "Scenarios" block contains a table with allll of the fields defined by <> in the outline. As you can see, it seems to me to be overly complicated, overly verbose, and perhaps doing more than it should be. I think maybe this test goes overboard...but what level of detail is good for error-path testing? -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Fri Jul 24 00:54:08 2009 From: lists at ruby-forum.com (Nathan Benes) Date: Fri, 24 Jul 2009 06:54:08 +0200 Subject: [rspec-users] How detailed should 'error' scenarios be? Message-ID: <59f4543d55060d9e49442c33138b67b9@ruby-forum.com> I'm fairly new to cucumber and rspec but so far am falling in love with both. I've read up on several different articles concerning these testing tools including the 'beta' BDD rspec/cucumber book. I saw this thread here: http://www.ruby-forum.com/topic/183428 Which concerns how small or detailed scenarios should be - and the basic gist was to keep scenarios at a higher level and leave the detailed form fields etc to rspec specs. This was all wonderfully insightful but it brings me to another question. How detailed (or should they be included in cucumber tests at all?) should the error path be? "Happy paths" are great to test, but it's also necessary to test error paths so that users aren't encountering rails stack traces, empty feedback, etc. Should there be one scenario per "empty" field? Should there be one scenario per validation check per field? Should it be condensed to a single scenario that attempts to encompass all error paths? I have one specific, seemingly overly complicated scenario that attempts to go the one scenario per validation check per field route: Scenario Outline: Add account with invalid fields Given I am logged in as BudgetTest And I am on the New Account page When I fill in the form for account "" with valid data And I submit the data "" for the "" field, "" And I submit the data "" for the "" field, "" And I press "Add Account" Then I should see the rendered template for "the new account page" And I should see an error indicating "" was "" I've removed the Scenarios: blocks because they would wordwrap and look terrible/undreadable. Following this is two sets of scenarios: Scenarios: missing required fields Scenarios: submitting bad data Some of the fields compare data with each other to determine validity which is why there's two data entries in the scenario outline. If the second is left blank then the defaults that were set in "When I fill in the form..." are used for it. Each "Scenarios" block contains a table with allll of the fields defined by <> in the outline. As you can see, it seems to me to be overly complicated, overly verbose, and perhaps doing more than it should be. I think maybe this test goes overboard...but what level of detail is good for error-path testing? -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Fri Jul 24 00:59:04 2009 From: lists at ruby-forum.com (Nathan Benes) Date: Fri, 24 Jul 2009 06:59:04 +0200 Subject: [rspec-users] How detailed should 'error' scenarios be? Message-ID: <0f14ddd30b80f4898ee2fbca4573221e@ruby-forum.com> I'm fairly new to cucumber and rspec but so far am falling in love with both. I've read up on several different articles concerning these testing tools including the 'beta' BDD rspec/cucumber book. I saw this thread here: http://www.ruby-forum.com/topic/183428 Which concerns how small or detailed specs and scenarios should be - and the basic gist was to keep scenarios at a higher level and leave the detailed form fields etc to rspec specs. This was all wonderfully insightful but it brings me to another question. How detailed (or should they be included in cucumber tests at all?) should the error path be? "Happy paths" are great to test, but it's also necessary to test error paths so that users aren't encountering rails stack traces, empty feedback, etc. Should there be one scenario per "empty" field? Should there be one scenario per validation check per field? Should it be condensed to a single scenario that attempts to encompass all error paths? I have one specific, seemingly overly complicated scenario that attempts to go the one scenario per validation check per field route: Scenario Outline: Add account with invalid fields Given I am logged in as BudgetTest And I am on the New Account page When I fill in the form for account "" with valid data And I submit the data "" for the "" field, "" And I submit the data "" for the "" field, "" And I press "Add Account" Then I should see the rendered template for "the new account page" And I should see an error indicating "" was "" I've removed the Scenarios: blocks because they would wordwrap and look terrible/undreadable. Following this is two sets of scenarios: Scenarios: missing required fields Scenarios: submitting bad data Some of the fields compare data with each other to determine validity which is why there's two data entries in the scenario outline. If the second is left blank then the defaults that were set in "When I fill in the form..." are used for it. Each "Scenarios" block contains a table with allll of the fields defined by <> in the outline. As you can see, it seems to me to be overly complicated, overly verbose, and perhaps doing more than it should be. I think maybe this test goes overboard...but what level of detail is good for error-path testing? -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Fri Jul 24 01:04:37 2009 From: lists at ruby-forum.com (Nathan Benes) Date: Fri, 24 Jul 2009 07:04:37 +0200 Subject: [rspec-users] How much detail for error-path scenarios Message-ID: <7fe1d20a44f7ba1b5eed842ac1d2b610@ruby-forum.com> I'm fairly new to cucumber and rspec but so far am falling in love with both. I've read up on several different articles concerning these testing tools including the 'beta' BDD rspec/cucumber book. I saw this thread here: http://www.ruby-forum.com/topic/183428 Which concerns how small or detailed specs and scenarios should be - and the basic gist was to keep scenarios at a higher level and leave the detailed form fields etc to rspec specs. This was all wonderfully insightful but it brings me to another question. How detailed (or should they be included in cucumber tests at all?) should the error path be? "Happy paths" are great to test, but it's also necessary to test error paths so that users aren't encountering rails stack traces, empty feedback, etc. Should there be one scenario per "empty" field? Should there be one scenario per validation check per field? Should it be condensed to a single scenario that attempts to encompass all error paths? I have one specific, seemingly overly complicated scenario that attempts to go the one scenario per validation check per field route: Scenario Outline: Add account with invalid fields Given I am logged in as BudgetTest And I am on the New Account page When I fill in the form for account "" with valid data And I submit the data "" for the "" field, "" And I submit the data "" for the "" field, "" And I press "Add Account" Then I should see the rendered template for "the new account page" And I should see an error indicating "" was "" I've removed the Scenarios: blocks because they would wordwrap and look terrible/undreadable. Following this is two sets of scenarios: Scenarios: missing required fields Scenarios: submitting bad data Some of the fields compare data with each other to determine validity which is why there's two data entries in the scenario outline. If the second is left blank then the defaults that were set in "When I fill in the form..." are used for it. Each "Scenarios" block contains a table with allll of the fields defined by <> in the outline. As you can see, it seems to me to be overly complicated, overly verbose, and perhaps doing more than it should be. I think maybe this test goes overboard...but what level of detail is good for error-path testing? -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Fri Jul 24 01:27:42 2009 From: lists at ruby-forum.com (Nathan Benes) Date: Fri, 24 Jul 2009 07:27:42 +0200 Subject: [rspec-users] How detailed should error scenarios be? Message-ID: <7f3d2246ab54ad81996b502fc8d4a348@ruby-forum.com> I'm fairly new to cucumber and rspec but so far am falling in love with both. I've read up on several different articles concerning these testing tools including the 'beta' BDD rspec/cucumber book. I saw this thread here: http://www.ruby-forum.com/topic/183428 Which concerns how small or detailed specs and scenarios should be - and the basic gist was to keep scenarios at a higher level and leave the detailed form fields etc to rspec specs. This was all wonderfully insightful but it brings me to another question. How detailed (or should they be included in cucumber tests at all?) should the error path be? "Happy paths" are great to test, but it's also necessary to test error paths so that users aren't encountering rails stack traces, empty feedback, etc. Should there be one scenario per "empty" field? Should there be one scenario per validation check per field? Should it be condensed to a single scenario that attempts to encompass all error paths? I have one specific, seemingly overly complicated scenario that attempts to go the one scenario per validation check per field route: Scenario Outline: Add account with invalid fields Given I am logged in as BudgetTest And I am on the New Account page When I fill in the form for account "" with valid data And I submit the data "" for the "" field, "" And I submit the data "" for the "" field, "" And I press "Add Account" Then I should see the rendered template for "the new account page" And I should see an error indicating "" was "" I've removed the Scenarios: blocks because they would wordwrap and look terrible/undreadable. Following this is two sets of scenarios: Scenarios: missing required fields Scenarios: submitting bad data Some of the fields compare data with each other to determine validity which is why there's two data entries in the scenario outline. If the second is left blank then the defaults that were set in "When I fill in the form..." are used for it. Each "Scenarios" block contains a table with allll of the fields defined by <> in the outline. As you can see, it seems to me to be overly complicated, overly verbose, and perhaps doing more than it should be. I think maybe this test goes overboard...but what level of detail is good for error-path testing? -- Posted via http://www.ruby-forum.com/. From ben at benmabey.com Fri Jul 24 03:10:40 2009 From: ben at benmabey.com (Ben Mabey) Date: Fri, 24 Jul 2009 01:10:40 -0600 Subject: [rspec-users] How detailed should error scenarios be? In-Reply-To: <7f3d2246ab54ad81996b502fc8d4a348@ruby-forum.com> References: <7f3d2246ab54ad81996b502fc8d4a348@ruby-forum.com> Message-ID: <4A695E70.6020702@benmabey.com> Nathan Benes wrote: > I'm fairly new to cucumber and rspec but so far am falling in love with > both. I've read up on several different articles concerning these > testing tools including the 'beta' BDD rspec/cucumber book. > > I saw this thread here: http://www.ruby-forum.com/topic/183428 > > Which concerns how small or detailed specs and scenarios should be - and > the basic gist was to keep scenarios at a higher level and leave the > detailed form > fields etc to rspec specs. > > This was all wonderfully insightful but it brings me to another > question. How detailed (or should they be included in cucumber tests at > all?) should the error path be? "Happy paths" are great to test, but > it's also necessary to test error paths so that users aren't > encountering rails stack traces, empty feedback, etc. > > Should there be one scenario per "empty" field? Should there be one > scenario per validation check per field? Should it be condensed to a > single scenario that attempts to encompass all error paths? > > I have one specific, seemingly overly complicated scenario that attempts > to go the one scenario per validation check per field route: > > Scenario Outline: Add account with invalid fields > Given I am logged in as BudgetTest > And I am on the New Account page > When I fill in the form for account "" with valid data > And I submit the data "" for the "" field, > "" > And I submit the data "" for the "" field, > "" > And I press "Add Account" > Then I should see the rendered template for "the new account page" > And I should see an error indicating "" was "" > > > I've removed the Scenarios: blocks because they would wordwrap and look > terrible/undreadable. Following this is two sets of scenarios: > > Scenarios: missing required fields > > Scenarios: submitting bad data > > Some of the fields compare data with each other to determine validity > which is why there's two data entries in the scenario outline. If the > second is left blank then the defaults that were set in "When I fill in > the form..." are used for it. Each "Scenarios" block contains a table > with allll of the fields defined by <> in the outline. As you can see, > it seems to me to be overly complicated, overly verbose, and perhaps > doing more than it should be. > > I think maybe this test goes overboard...but what level of detail is > good for error-path testing? > Hi Nathan, I think testing all of the validations from Cucumber is going overboard *unless* the customer wants to see them there. I have done something like that but it was when I was writing my own validation logic (in this case I wasn't even using AR or another standard library). It was a nice way of showing the customer what were and were not valid values. However, if you are using ActiveRecord to do standard validations then I see little value in checking each validation separately. It will take a long time to run and it seems like it would really not add much value in terms of regression catching beyond what the fast model-level code examples would provide. So, like I said, the only value I would see in that would be for a customer to see that it is working as they want it to. In general, I like to keep happy paths in Cucumber features along with some special error cases. Driving though the entire stack is a double-edged sword. It provides great regression testing and is an excellent way to frame scenarios for customers. However, the number of execution paths you can execute from such a high level is inherently limited... the number of objects and all of there potential states in concert with one another results in a combinatorial explosion of number of test cases required to test everything. That is why isolated specs on a lower level are still very valuable and for me provide a great deal of confidence when used along side some happy-path full-stack tests. (And even then you can never test everything...) Thats my current take on things at least... HTH, Ben From matt at mattwynne.net Fri Jul 24 03:52:01 2009 From: matt at mattwynne.net (Matt Wynne) Date: Fri, 24 Jul 2009 08:52:01 +0100 Subject: [rspec-users] How detailed should error scenarios be? In-Reply-To: <7f3d2246ab54ad81996b502fc8d4a348@ruby-forum.com> References: <7f3d2246ab54ad81996b502fc8d4a348@ruby-forum.com> Message-ID: On 24 Jul 2009, at 05:59, Nathan Benes wrote: > I'm fairly new to cucumber and rspec but so far am falling in love > with > both. I've read up on several different articles concerning these > testing tools including the 'beta' BDD rspec/cucumber book. > > I saw this thread here: http://www.ruby-forum.com/topic/183428 > > Which concerns how small or detailed specs and scenarios should be - > and > the basic gist was to keep scenarios at a higher level and leave the > detailed form > fields etc to rspec specs. > > This was all wonderfully insightful but it brings me to another > question. How detailed (or should they be included in cucumber > tests at > all?) should the error path be? "Happy paths" are great to test, but > it's also necessary to test error paths so that users aren't > encountering rails stack traces, empty feedback, etc. > > Should there be one scenario per "empty" field? Should there be one > scenario per validation check per field? Should it be condensed to a > single scenario that attempts to encompass all error paths? > > I have one specific, seemingly overly complicated scenario that > attempts > to go the one scenario per validation check per field route: > > Scenario Outline: Add account with invalid fields > Given I am logged in as BudgetTest > And I am on the New Account page > When I fill in the form for account "" with valid > data > And I submit the data "" for the "" field, > "" > And I submit the data "" for the "" field, > "" > And I press "Add Account" > Then I should see the rendered template for "the new account page" > And I should see an error indicating "" was "" > > > I've removed the Scenarios: blocks because they would wordwrap and > look > terrible/undreadable. Following this is two sets of scenarios: > > Scenarios: missing required fields > > Scenarios: submitting bad data > > Some of the fields compare data with each other to determine validity > which is why there's two data entries in the scenario outline. If the > second is left blank then the defaults that were set in "When I fill > in > the form..." are used for it. Each "Scenarios" block contains a table > with allll of the fields defined by <> in the outline. As you can > see, > it seems to me to be overly complicated, overly verbose, and perhaps > doing more than it should be. > > I think maybe this test goes overboard...but what level of detail is > good for error-path testing? The question is difficult to answer as you'll eventually find your own balance with this from experience. As Ben said, Cucumber scenarios are best applied for 'broad brush' scenarios that stakeholders care about. Although the stakeholders obviously care that this form renders error messages when invalid values are submitted, you may well start to bore them when you get into the level of detail of specifying each field and the error message that goes along with it. So in this instance, it might well be OK to have a cuke that tells you whether or not validation is being invoked at all, and then rely on specs to detail the way that validation is working. Equally, your stakeholder / customer may be the type of person who will trust you better if they can see all the edge cases being explored in the cukes. So it depends a lot on the context you're working in. If so you can use some of the features of Cucumber like scenario outlines and step tables to make these kind of repetitive cukes more readable. I'd advise you do go and check out those features on the wiki. Finding and testing for failure cases is something that most teams are pretty bad at - unless we have someone of that 'constructive destruction' mindset on the team, we often miss these scenarios the first time we build a feature. Steve McConnell's 'Code Complete' reckons on something like a 5:1 ratio of sad:happy path tests being produced by mature test teams, so you're on the right lines by thinking about them! What you might find is that writing high-level specs (i.e. cukes) for the more obvious failure cases will mean you are distracted from seeing other ones. What if someone tries to type some javascript or HTML into one of these boxes, for example? cheers, Matt +447974 430184 matt at mattwynne.net http://mattwynne.net From celoserpa at gmail.com Fri Jul 24 12:51:32 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Fri, 24 Jul 2009 11:51:32 -0500 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> <4A690347.7040609@benmabey.com> Message-ID: <1e5bcefd0907240951y6728dd0fha5db48731ab27134@mail.gmail.com> Thanks David and Ben for the comprehensive replies! I really appreciate it :) These wikipedia articles helped me to understand on the more conceptual level: http://en.wikipedia.org/wiki/Method_stub http://en.wikipedia.org/wiki/Mock_object So, if I understand it right -- Stub is for methods and Mocks are for roles (or, in the technical-level -- objects). In the context of rSpec/rSpec-Rails, however, the API makes it confusing, to be honest. Being able to create a Mock with Spec::Mocks:Mock with Spec::Mocks::ExampleMethods# confuses me. The same applies for stub_model() and mock_model(). I know that mock_model is a convenience method that can be used to create an object with an API similar to ActiveRecord::Base's. The API says stub_model is basically the same, but prevents the object from accessing the database. Ok, but why attaching these concepts to Stub? Is this the right name to use for this method? (Stub == mock object that is not allowed to access the database?). And there's the Spec::Mocks::Methods#stub method, which is the one that reflects the concept that "the Stub is a dummy method". So, in sum, my perception is that rSpec/rSpec-Rails implement all the functionality needed to implement Mocks and Stubs, but in a confusing way. IMHO, the API is not succinct in this aspect -- it does not communicate a clear distinction between Mocks and Stubs and this makes it hard for someone new to TDD/BDD to use them in the right way or decide the best way to use them. My 0.4 cents of Brazilian Reals. @David -- yes, I'm Brazilian! You've got a good gut! And I'm glad to see that you know Portuguese! :) Marcelo. On Thu, Jul 23, 2009 at 8:47 PM, David Chelimsky wrote: > On Jul 23, 7:41?pm, Ben Mabey wrote: >> Marcelo de Moraes Serpa wrote:> Hello list, >> >> > >From what I could see, the lines between mocks and stubs are subtle, >> > but the general idea I got is that mocks set expectations and stubs >> > are only dummy objects/method calls. What confused me though, is that >> > the stub() method is an alias for mock() in >> > Spec::Mocks::ExampleMethods. So a stub is a mock? >> >> Yes, no, and maybe so. :p ?I agree with your general understanding that >> "mocks set expectations and stubs are only dummy objects/method calls". >> The fact that rspec using the same machinery to provide stubs and mocks >> is really inconsequential because the deciding factor of what they are >> is how you use them. ?You can, IMO, use a "mock" like a stub and a >> "stub" like a mock.. Take this for example: >> >> describe "#some_method" do >> ? it "delegates to some_obejct" do >> ? ? some_object = stub('some object', :some_method => "foo") >> ? ? my_object = MyObject.new(some_object) >> >> ? ? my_object.some_method.should == "foo" >> ? end >> end >> >> We are using a stub as a dummy object, and yet our expectation is >> clearing testing the method call. ?So is the above a stub or is it >> really a mock? ?I would say that it is acting like a mock. ?I hope that >> others on the list will correct me if I am wrong making this >> observation/conclusion. ? Likewise we could use a mock as a dummy object >> and never set any expectations on it and it would be acting like a >> stub. ?Even though stub and mock is the same in RSpec you should still >> use the correct name when using them. >> >> > Also, another thing that is confusing: You have stub and stub! methods >> > in the Spec::Mocks::Methods namespace, what is the difference between >> > Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ? >> >> Spec::Mocks::ExampleMethods are the methods that you can call during the course of a code example. ?So when you say stub("something") the method in Spec::Mocks::ExampleMethods gets called and returns you a stub. ?Now, that stub object now has it's own #stub method which lives on Spec::Mocks::Methods#stub. ?That call will stub out the provided method and return value... So my_stub.stub(:foo, "bar"), however that is aliased from #stub! which is used most often. ?This is all from memory so I could be wrong but that is the general gist of it. >> >> HTH, >> -Ben > > Ben and I are secretly the same person. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From celoserpa at gmail.com Fri Jul 24 12:53:42 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Fri, 24 Jul 2009 11:53:42 -0500 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: <1e5bcefd0907240951y6728dd0fha5db48731ab27134@mail.gmail.com> References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> <4A690347.7040609@benmabey.com> <1e5bcefd0907240951y6728dd0fha5db48731ab27134@mail.gmail.com> Message-ID: <1e5bcefd0907240953p41362d1p1152215a419449a7@mail.gmail.com> >In the context of rSpec/rSpec-Rails, however, the API makes it >confusing, to be honest. Being able to create a Mock with >Spec::Mocks:Mock with Spec::Mocks::ExampleMethods# confuses me. Ops! In this paragraph I forgot to put the instance method name: It should read Spec::Mocks::ExampleMethods#stub Thanks, Marcelo. On Fri, Jul 24, 2009 at 11:51 AM, Marcelo de Moraes Serpa wrote: > Thanks David and Ben for the comprehensive replies! I really appreciate it :) > > These wikipedia articles helped me to understand on the more conceptual level: > > http://en.wikipedia.org/wiki/Method_stub > http://en.wikipedia.org/wiki/Mock_object > > So, if I understand it right -- Stub is for methods and Mocks are for > roles (or, in the technical-level -- objects). > > In the context of rSpec/rSpec-Rails, however, ?the API makes it > confusing, to be honest. Being able to create a Mock with > Spec::Mocks:Mock with Spec::Mocks::ExampleMethods# confuses me. > > The same applies for stub_model() and mock_model(). I know that > mock_model is a convenience method that can be used to create an > object with an API similar to ActiveRecord::Base's. The API says > stub_model is basically the same, but prevents the object from > accessing the database. Ok, but why attaching these concepts to Stub? > Is this the right name to use for this method? (Stub == mock object > that is not allowed to access the database?). > > And there's the ?Spec::Mocks::Methods#stub method, which is the one > that reflects the concept that "the Stub is a dummy method". > > So, in sum, my perception is that rSpec/rSpec-Rails implement all the > functionality needed to implement Mocks and Stubs, ?but in a confusing > way. IMHO, the API is not succinct in this aspect -- it does not > communicate a clear distinction between Mocks and Stubs and this makes > it hard for someone new to TDD/BDD to use them in the right way or > decide the best way to use them. > > My 0.4 cents of Brazilian Reals. > > @David -- yes, I'm Brazilian! You've got a good gut! And I'm glad to > see that you know Portuguese! :) > > Marcelo. > > On Thu, Jul 23, 2009 at 8:47 PM, David Chelimsky wrote: >> On Jul 23, 7:41?pm, Ben Mabey wrote: >>> Marcelo de Moraes Serpa wrote:> Hello list, >>> >>> > >From what I could see, the lines between mocks and stubs are subtle, >>> > but the general idea I got is that mocks set expectations and stubs >>> > are only dummy objects/method calls. What confused me though, is that >>> > the stub() method is an alias for mock() in >>> > Spec::Mocks::ExampleMethods. So a stub is a mock? >>> >>> Yes, no, and maybe so. :p ?I agree with your general understanding that >>> "mocks set expectations and stubs are only dummy objects/method calls". >>> The fact that rspec using the same machinery to provide stubs and mocks >>> is really inconsequential because the deciding factor of what they are >>> is how you use them. ?You can, IMO, use a "mock" like a stub and a >>> "stub" like a mock.. Take this for example: >>> >>> describe "#some_method" do >>> ? it "delegates to some_obejct" do >>> ? ? some_object = stub('some object', :some_method => "foo") >>> ? ? my_object = MyObject.new(some_object) >>> >>> ? ? my_object.some_method.should == "foo" >>> ? end >>> end >>> >>> We are using a stub as a dummy object, and yet our expectation is >>> clearing testing the method call. ?So is the above a stub or is it >>> really a mock? ?I would say that it is acting like a mock. ?I hope that >>> others on the list will correct me if I am wrong making this >>> observation/conclusion. ? Likewise we could use a mock as a dummy object >>> and never set any expectations on it and it would be acting like a >>> stub. ?Even though stub and mock is the same in RSpec you should still >>> use the correct name when using them. >>> >>> > Also, another thing that is confusing: You have stub and stub! methods >>> > in the Spec::Mocks::Methods namespace, what is the difference between >>> > Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ? >>> >>> Spec::Mocks::ExampleMethods are the methods that you can call during the course of a code example. ?So when you say stub("something") the method in Spec::Mocks::ExampleMethods gets called and returns you a stub. ?Now, that stub object now has it's own #stub method which lives on Spec::Mocks::Methods#stub. ?That call will stub out the provided method and return value... So my_stub.stub(:foo, "bar"), however that is aliased from #stub! which is used most often. ?This is all from memory so I could be wrong but that is the general gist of it. >>> >>> HTH, >>> -Ben >> >> Ben and I are secretly the same person. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > From ben at benmabey.com Fri Jul 24 15:41:40 2009 From: ben at benmabey.com (Ben Mabey) Date: Fri, 24 Jul 2009 13:41:40 -0600 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: <1e5bcefd0907240951y6728dd0fha5db48731ab27134@mail.gmail.com> References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> <4A690347.7040609@benmabey.com> <1e5bcefd0907240951y6728dd0fha5db48731ab27134@mail.gmail.com> Message-ID: <4A6A0E74.20801@benmabey.com> Marcelo de Moraes Serpa wrote: > Thanks David and Ben for the comprehensive replies! I really appreciate it :) > > These wikipedia articles helped me to understand on the more conceptual level: > > http://en.wikipedia.org/wiki/Method_stub > http://en.wikipedia.org/wiki/Mock_object > > So, if I understand it right -- Stub is for methods and Mocks are for > roles (or, in the technical-level -- objects). > > In the context of rSpec/rSpec-Rails, however, the API makes it > confusing, to be honest. Being able to create a Mock with > Spec::Mocks:Mock with Spec::Mocks::ExampleMethods# confuses me. > Hmm.. I can see your point as far as where the methods live being confusing. Reading about stub method in the RDoc pages for mock methods does seem odd. The reason, as David, pointed out is really more due to reducing code duplication between the two. Any suggestions on making it better? > The same applies for stub_model() and mock_model(). I know that > mock_model is a convenience method that can be used to create an > object with an API similar to ActiveRecord::Base's. The API says > stub_model is basically the same, but prevents the object from > accessing the database. Ok, but why attaching these concepts to Stub? > Is this the right name to use for this method? (Stub == mock object > that is not allowed to access the database?). > I think you raise a good question whether or not stub_model is a best name for the functionality it provides. I think stub_model is really called a partial stub.. or partial mock. Hmm.. I'm not quite sure what the correct name is. But basically, as you said, it is the real object but is not allowed to touch the DB. So it is there for performance reasons mostly. mock_model however gives you a dummy object with some predefined stubbed methods that you typically want for an AR object (i.e. #id). However, it is very true that in many cases you will be using this "mock_model" as a stub. I can see how having stub_model can cause confusion... Perhaps, a better API would be to alias stub_model to mock_model (this would be more aligned with the #mock and #stub API) and then have a partial_stub_model or stub_model(Model, :partial => true)? Changing it now I'm sure would make some people upset but it could be done with deprecation warnings if it is causing that much confusion... This would really be David's call to make and I'm guessing he had some good reasons for making the decision he did in the first place. > And there's the Spec::Mocks::Methods#stub method, which is the one > that reflects the concept that "the Stub is a dummy method". > Again, this is due to the implementation.. I see your point, but I don't know if the typical user really cares where it lives. Perhaps a little explanation in the RDoc would be sufficient to avoid confusion? -Ben From andy.james.pearson at googlemail.com Fri Jul 24 17:33:03 2009 From: andy.james.pearson at googlemail.com (Andy Pearson) Date: Fri, 24 Jul 2009 22:33:03 +0100 Subject: [rspec-users] Errors when running AUTOFEATURE=true autospec [cucumber, autotest] References: <834053C3-F615-43B6-8720-99C2E061D98E@andy-pearson.com> Message-ID: <9C79530D-5D0B-47FF-AA77-869960A86283@googlemail.com> Hey List, Hope all is well, I haven't been keeping up with the list whilst I wasn't doing much Rails work but I'm back in the game and looking forward to seeing how far things have progressed! I think I have probably mentioned this already on the list, but just to reiterate: I am a beginner! I have installed the new Rspec and Cucumber for use with Rails, and did what needed to be done to update libxml2 for use with nokogiri and updated to the latest ZenTest but I am still seeing the following stack trace when running `AUTOFEATURE=true autospec` Warning: $KCODE is NONE. /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/ step_definition.rb:100: warning: method redefined; discarding old regexp /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/ruby_extensions/ string.rb:31: warning: method redefined; discarding old indent /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ node_classes/character_class.rb:13: warning: useless use of a literal in void context /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler.rb:5: warning: global variable `$exclude_metagrammar' not initialized /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:232: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:388: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:771: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:779: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:28: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:36: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:679: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:687: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:813: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:825: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/active_support/ core_ext/blank.rb:49: warning: method redefined; discarding old blank? Is this something to be concerned about? The feature examples still seem to run but I'm not sure whether I will get problems down the line. Thanks for any help you can offer, Andy From ben at benmabey.com Fri Jul 24 17:48:31 2009 From: ben at benmabey.com (Ben Mabey) Date: Fri, 24 Jul 2009 15:48:31 -0600 Subject: [rspec-users] How detailed should error scenarios be? In-Reply-To: <4A695E70.6020702@benmabey.com> References: <7f3d2246ab54ad81996b502fc8d4a348@ruby-forum.com> <4A695E70.6020702@benmabey.com> Message-ID: <4A6A2C2F.2080609@benmabey.com> Ben Mabey wrote: > Nathan Benes wrote: >> I'm fairly new to cucumber and rspec but so far am falling in love with >> both. I've read up on several different articles concerning these >> testing tools including the 'beta' BDD rspec/cucumber book. >> >> I saw this thread here: http://www.ruby-forum.com/topic/183428 >> >> Which concerns how small or detailed specs and scenarios should be - and >> the basic gist was to keep scenarios at a higher level and leave the >> detailed form >> fields etc to rspec specs. >> >> This was all wonderfully insightful but it brings me to another >> question. How detailed (or should they be included in cucumber tests at >> all?) should the error path be? "Happy paths" are great to test, but >> it's also necessary to test error paths so that users aren't >> encountering rails stack traces, empty feedback, etc. >> >> Should there be one scenario per "empty" field? Should there be one >> scenario per validation check per field? Should it be condensed to a >> single scenario that attempts to encompass all error paths? >> >> I have one specific, seemingly overly complicated scenario that attempts >> to go the one scenario per validation check per field route: >> >> Scenario Outline: Add account with invalid fields >> Given I am logged in as BudgetTest >> And I am on the New Account page >> When I fill in the form for account "" with valid data >> And I submit the data "" for the "" field, >> "" >> And I submit the data "" for the "" field, >> "" >> And I press "Add Account" >> Then I should see the rendered template for "the new account page" >> And I should see an error indicating "" was "" >> >> >> I've removed the Scenarios: blocks because they would wordwrap and look >> terrible/undreadable. Following this is two sets of scenarios: >> >> Scenarios: missing required fields >> >> Scenarios: submitting bad data >> >> Some of the fields compare data with each other to determine validity >> which is why there's two data entries in the scenario outline. If the >> second is left blank then the defaults that were set in "When I fill in >> the form..." are used for it. Each "Scenarios" block contains a table >> with allll of the fields defined by <> in the outline. As you can see, >> it seems to me to be overly complicated, overly verbose, and perhaps >> doing more than it should be. >> >> I think maybe this test goes overboard...but what level of detail is >> good for error-path testing? >> > > Hi Nathan, > I think testing all of the validations from Cucumber is going > overboard *unless* the customer wants to see them there. I have done > something like that but it was when I was writing my own validation > logic (in this case I wasn't even using AR or another standard > library). It was a nice way of showing the customer what were and were > not valid values. However, if you are using ActiveRecord to do > standard validations then I see little value in checking each > validation separately. It will take a long time to run and it seems > like it would really not add much value in terms of regression > catching beyond what the fast model-level code examples would provide. > So, like I said, the only value I would see in that would be for a > customer to see that it is working as they want it to. > > In general, I like to keep happy paths in Cucumber features along with > some special error cases. Driving though the entire stack is a > double-edged sword. It provides great regression testing and is an > excellent way to frame scenarios for customers. However, the number > of execution paths you can execute from such a high level is > inherently limited... the number of objects and all of there potential > states in concert with one another results in a combinatorial > explosion of number of test cases required to test everything. That > is why isolated specs on a lower level are still very valuable and for > me provide a great deal of confidence when used along side some > happy-path full-stack tests. (And even then you can never test > everything...) > > Thats my current take on things at least... HTH, > > Ben > BTW, please use the cucumber mailing list for cucumber related questions: http://groups.google.com/group/cukes -Ben From dchelimsky at gmail.com Fri Jul 24 17:56:29 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 24 Jul 2009 16:56:29 -0500 Subject: [rspec-users] unsubscribe In-Reply-To: <29a0119e0907221134m616899b1ie7241dd3ac57fa1b@mail.gmail.com> References: <29a0119e0907221134m616899b1ie7241dd3ac57fa1b@mail.gmail.com> Message-ID: <57c63afe0907241456i577a2fc3gf945f8f1c383aad1@mail.gmail.com> Hey Bryan, Two problems with this request: 1 - it goes to the mailing list, not the admin interface 2 - you're trying to unsubscribe bryansray at gmail.com, but you registered bryansray+rspecUsers at gmail.com. Please go to the http://rubyforge.org/mailman/listinfo/rspec-users and log in using bryansray+rspecUsers at gmail.com and you should be able to unsubscribe there. Good luck, David On Wed, Jul 22, 2009 at 1:34 PM, Bryan Ray wrote: > unsubscribe > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From rick.denatale at gmail.com Fri Jul 24 20:16:51 2009 From: rick.denatale at gmail.com (Rick DeNatale) Date: Fri, 24 Jul 2009 20:16:51 -0400 Subject: [rspec-users] Blue-ridge Message-ID: This might be slightly off-topic, but I thought that it might be of interest for those who aren't already aware. Earlier this week, Larry Karnowski and Jason Rudolph of Relevance, gave a talk on blue-ridge http://www.screwcumber.com/ Blue-ridge is a rails plugin which incorporates several JavaScript testing and execution goodies Screw.Unit which despite the name is a very RSpec like bdd library for JavaScript Smoke - a mocha like JS mock library Rhino - a JVM based JS implementation env.js - A JS DOM implementation Blue-ridge lets you generate spec templates, and run them from the command line or with a textmate bundle. The generators also produce html files which allow the specs to be run in the browser(s) of your choice. Nice stuff. During the talk, I laid claim on the name ScrewCumber for the next step, although I was quite surprised that it's already trademarked, albeit for a different field. http://www.screwcumber.com/ -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale From rick.denatale at gmail.com Fri Jul 24 20:27:34 2009 From: rick.denatale at gmail.com (Rick DeNatale) Date: Fri, 24 Jul 2009 20:27:34 -0400 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: <4A690347.7040609@benmabey.com> References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> <4A690347.7040609@benmabey.com> Message-ID: On Thu, Jul 23, 2009 at 8:41 PM, Ben Mabey wrote: > You can, IMO, use a "mock" like a stub and a "stub" like a > mock.. Take this for example: > > describe "#some_method" do > ?it "delegates to some_obejct" do > ? some_object = stub('some object', :some_method => "foo") > ? my_object = MyObject.new(some_object) > > ? my_object.some_method.should == "foo" > ?end > end > > We are using a stub as a dummy object, and yet our expectation is clearing > testing the method call. ?So is the above a stub or is it really a mock? ?I > would say that it is acting like a mock. ?I hope that others on the list > will correct me if I am wrong making this observation/conclusion. Well, I'm not sure. There's a difference here. The stub simply sets things up so that IF some_object receives :some_method it will return 'foo' instead of something else (including a method_missing error). If the implementation looks like: class MyObject def initialize(obj) end def some_method "foo" end end Then the stub object proves nothing, the example will succeed whether it's there or not. On the other hand using a mock and setting a message expectation asserts something about the implementation of some_method and it's relationship to the intialize method. It's more gray-box than black-box. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale From codehacker at comcast.net Fri Jul 24 20:13:04 2009 From: codehacker at comcast.net (norm) Date: Fri, 24 Jul 2009 17:13:04 -0700 (PDT) Subject: [rspec-users] basic noob questions Message-ID: Struggling mightily! Testing a controller called Govtpositions. Just focusing on testing the create method for the moment. I would imagine there should be two examples for a successful save... instantiate the model object and then save it, is that right? So just to test the model object instantiation, I have: describe GovtpositionsController, do before do Govtposition.stub!(:new).and_return(@govtposition) end it "should create a new govt position model" do @govtposition = mock_model(Govtposition) @params = {"name"=>"chief"} Govtposition.should_receive(:new).with(@params).and_return (@govtposition) end end This fails, with the reason that @govtposition is receiving an unexpected :save. Well I didn't intend to test save, I was planning to write another example for that assertion. Why can't I just test object creation in it's own example, must I also test saving in the same example? I'm clearly missing something basic! thanks in advance for any insight you can offer From codehacker at comcast.net Fri Jul 24 21:40:13 2009 From: codehacker at comcast.net (norm) Date: Fri, 24 Jul 2009 18:40:13 -0700 (PDT) Subject: [rspec-users] basic noob question Message-ID: <2eb450d4-b4c4-4b45-a5aa-8819990ac389@l5g2000pra.googlegroups.com> I'm struggling mightily! I have a controller called Govtpositions, and I'm just trying to test the standard Rails 'create' method. So it seems as if three examples would be appropriate... one to test the instantiation of the new model from the params hash, another to test that it was saved, and then the final redirection. So just to test the model instantiation, I have: describe GovtpositionsController, "POST govtpositions to create govt new government position" do before do Govtposition.stub!(:new).and_return(@govtposition) end it "should create a new govt position model" do @govtposition = mock_model(Govtposition) @params = {"name"=>"chief"} Govtposition.should_receive(:new).with(@params).and_return (@govtposition) end end But this example fails due to "Mock 'Govtposition_1001' received unexpected message :save with (no args)" Surely I should be able to test the save in a separate example, no? I was expecting to declare just one expectation per example, with a total of three for the create method. But the test failure seems to suggest otherwise. Any clarification you can offer would be much appreciated. From bj at aboutus.org Sat Jul 25 00:45:38 2009 From: bj at aboutus.org (BJ Clark) Date: Fri, 24 Jul 2009 21:45:38 -0700 Subject: [rspec-users] basic noob questions In-Reply-To: References: Message-ID: <33BC8120-695D-4F06-810C-FCBB5FF9AFD3@aboutus.org> Norm, I don't test instantiation and then saving, I test successful and unsuccessful creation. By testing this concern, I don't need to worry about expectations or mocks, I actually create the object (or not!) and then test that the controller behaves the way I expect it to in these situations. Its probably not important that Govtpostion receives "new". It's important that when it receives new with the proper things, and then saves, that it behaves a certain way. And when it doesn't receive the proper things, it acts another way. Hope that helps, BJ Clark On Jul 24, 2009, at 5:13 PM, norm wrote: > Struggling mightily! > > Testing a controller called Govtpositions. Just focusing on testing > the create method for the moment. I would imagine there should be two > examples for a successful save... instantiate the model object and > then save it, is that right? > > So just to test the model object instantiation, I have: > > describe GovtpositionsController, do > before do > Govtposition.stub!(:new).and_return(@govtposition) > end > > it "should create a new govt position model" do > @govtposition = mock_model(Govtposition) > @params = {"name"=>"chief"} > Govtposition.should_receive(:new).with(@params).and_return > (@govtposition) > end > > end > > This fails, with the reason that @govtposition is receiving an > unexpected :save. Well I didn't intend to test save, I was planning to > write another example for that assertion. Why can't I just test object > creation in it's own example, must I also test saving in the same > example? I'm clearly missing something basic! > > thanks in advance for any insight you can offer > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users ----- BJ Clark AboutUs.Org From dchelimsky at gmail.com Sat Jul 25 00:52:44 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 24 Jul 2009 23:52:44 -0500 Subject: [rspec-users] basic noob questions In-Reply-To: References: Message-ID: <57c63afe0907242152w641caee5u871e6710a04f9c9d@mail.gmail.com> On Fri, Jul 24, 2009 at 7:13 PM, norm wrote: > Struggling mightily! > > Testing a controller called Govtpositions. Just focusing on testing > the create method for the moment. I would imagine there should be two > examples for a successful save... instantiate the model object and > then save it, is that right? > > So just to test the model object instantiation, I have: > > describe GovtpositionsController, do > ?before do > ? ?Govtposition.stub!(:new).and_return(@govtposition) > ?end > > ?it "should create a new govt position model" do > ? ?@govtposition = mock_model(Govtposition) > ? ?@params = {"name"=>"chief"} > ? ?Govtposition.should_receive(:new).with(@params).and_return > (@govtposition) > ?end > > end > > This fails, with the reason that @govtposition is receiving an > unexpected :save. Well I didn't intend to test save, Sure, but if the code does that, then you have to account for it. You can just stub save and not expect it. That's a perfect case for a method stub - something unimportant to your example, but necessary for the example to run. > I was planning to > write another example for that assertion. Why can't I just test object > creation in it's own example, must I also test saving in the same > example? I'm clearly missing something basic! > > thanks in advance for any insight you can offer Assuming you're dealing w/ a standard create action, IMO, you don't really need to spec the instantiation of the object. This is one of those gray areas in which you're touching implementation with stubs and mocks, but you're not really specifying implementation. i.e., the examples might read: GovtpositionsController POST create with valid data creates a new govtposition with the submitted data redirects to the list of govtpositions If you're using mocks/stubs for isolation and speed, you may need to stub :new if that's what the create method is using, but that's just there to get the example to execute - not because it's part of what's being specified. Make sense? Cheers, David From ben at benmabey.com Sat Jul 25 02:00:05 2009 From: ben at benmabey.com (Ben Mabey) Date: Sat, 25 Jul 2009 00:00:05 -0600 Subject: [rspec-users] On Mocks vs Stubs in the context of rSpec In-Reply-To: References: <1e5bcefd0907231639mbd7b6fj7477b232cc32c069@mail.gmail.com> <4A690347.7040609@benmabey.com> Message-ID: <4A6A9F65.1040104@benmabey.com> Rick DeNatale wrote: > On Thu, Jul 23, 2009 at 8:41 PM, Ben Mabey wrote: > >> You can, IMO, use a "mock" like a stub and a "stub" like a >> mock.. Take this for example: >> >> describe "#some_method" do >> it "delegates to some_obejct" do >> some_object = stub('some object', :some_method => "foo") >> my_object = MyObject.new(some_object) >> >> my_object.some_method.should == "foo" >> end >> end >> >> We are using a stub as a dummy object, and yet our expectation is clearing >> testing the method call. So is the above a stub or is it really a mock? I >> would say that it is acting like a mock. I hope that others on the list >> will correct me if I am wrong making this observation/conclusion. >> > > Well, I'm not sure. > > There's a difference here. > > The stub simply sets things up so that IF some_object receives > :some_method it will return 'foo' instead of something else (including > a method_missing error). > > If the implementation looks like: > > class MyObject > def initialize(obj) > end > > def some_method > "foo" > end > end > > Then the stub object proves nothing, the example will succeed whether > it's there or not. > > On the other hand using a mock and setting a message expectation > asserts something about the implementation of some_method and it's > relationship to the intialize method. It's more gray-box than > black-box. > True, but I think the difference is very small in this case. In the delegation example we want to ensure that the other method is called and that it's return value is returned. Adjusting your implementation slightly you could still make the message expectation version pass as well but still be a false-positive: class MyObject def initialize(obj) end def some_method some_object.some_method "foo" end end In both cases another example would be needed to point out the problem- each of which would rely on returning another canned response. Of course, using triangulation for something small as delegation is just plain silly. :) FWIW, I would set a message expectation when specifying delegation. However, to my point if a stub is being used in this manner I think it is crossing the line of being a dummy object and taking on more of a "mock"ish like role. Perhaps it is going overboard to say that it is being used as a mock, but the intent feels the same in this case IMO. -Ben From andy.james.pearson at googlemail.com Sat Jul 25 07:57:59 2009 From: andy.james.pearson at googlemail.com (Andy Pearson) Date: Sat, 25 Jul 2009 04:57:59 -0700 (PDT) Subject: [rspec-users] Errors when running AUTOFEATURE=true autospec [cucumber, autotest] In-Reply-To: <9C79530D-5D0B-47FF-AA77-869960A86283@googlemail.com> References: <834053C3-F615-43B6-8720-99C2E061D98E@andy-pearson.com> <9C79530D-5D0B-47FF-AA77-869960A86283@googlemail.com> Message-ID: Wanted to add some more information to this.... Since Cucumber seemed to be working, I created a feature and used it to drive the initial creation of a new controller. Once the route was done I dropped down to a controller spec and wrote the controller code that makes it pass. Now when I run AUTOFEATURE=true autospec it dies with the following stack trace: Warning: $KCODE is NONE. /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/ step_definition.rb:100: warning: method redefined; discarding old regexp /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/ruby_extensions/ string.rb:31: warning: method redefined; discarding old indent /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ node_classes/character_class.rb:13: warning: useless use of a literal in void context /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler.rb:5: warning: global variable `$exclude_metagrammar' not initialized /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:232: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:388: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:771: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ metagrammar.rb:779: warning: method redefined; discarding old space /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:28: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:36: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:679: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:687: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:813: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ feature.rb:825: warning: method redefined; discarding old white /Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/active_support/ core_ext/blank.rb:49: warning: method redefined; discarding old blank? /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby / Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/bin/spec spec/ controllers/lifestream_controller_spec.rb -O spec/spec.opts /Library/Ruby/Gems/1.8/gems/rspec-1.2.8/lib/spec/autorun.rb:3: undefined method `autorun' for Spec::Runner:Module (NoMethodError) from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require' from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/ active_support/dependencies.rb:158:in `require' from ./spec/controllers/../spec_helper.rb:5 from ./spec/controllers/lifestream_controller_spec.rb:1:in `require' from ./spec/controllers/lifestream_controller_spec.rb:1 from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ runner/example_group_runner.rb:15:in `load' from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ runner/example_group_runner.rb:15:in `load_files' from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ runner/example_group_runner.rb:14:in `each' from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ runner/example_group_runner.rb:14:in `load_files' from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ runner/options.rb:94:in `run_examples' from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ runner/command_line.rb:9:in `run' from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/bin/spec:4 Odd thing is, rake spec gets me what I want: ... Finished in 0.071358 seconds 3 examples, 0 failures Based on this, my assumption is this is something to do with ZenTest, have I got an incompatible set of gems? I tried to pull out the most relevant ones: aslakhellesoy-webrat (0.3.2.1) autotest-rails (4.1.0) cucumber (0.3.90, 0.1.16, 0.1.15) nokogiri (1.3.2, 1.1.0) rails (2.3.3, 2.2.2, 2.2.0, 2.1.2, 2.1.1, 2.1.0, 2.0.991, 2.0.2, 2.0.1, 1.2.6, 1.2.3) rspec (1.2.8, 1.1.12, 1.1.11) rspec-rails (1.2.7.1, 1.1.12, 1.1.11) treetop (1.3.0, 1.2.4) webrat (0.4.4, 0.4.0, 0.3.4, 0.2.0) ZenTest (4.1.3, 3.11.0, 3.10.0, 3.9.3, 3.9.2, 3.8.0, 3.6.1) Really hoping I can get some help with this, hitting a block like this gets super frustrating, particularly what I am trying to "do the right thing" by getting this up and running to I can BDD and hopefully write better software! Thanks again in advance, Andy From dchelimsky at gmail.com Sat Jul 25 08:28:52 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 25 Jul 2009 07:28:52 -0500 Subject: [rspec-users] Errors when running AUTOFEATURE=true autospec [cucumber, autotest] In-Reply-To: <9C79530D-5D0B-47FF-AA77-869960A86283@googlemail.com> References: <834053C3-F615-43B6-8720-99C2E061D98E@andy-pearson.com> <9C79530D-5D0B-47FF-AA77-869960A86283@googlemail.com> Message-ID: <86D74248-2CAE-43D7-B6B8-722C96F79AD8@gmail.com> On Jul 24, 2009, at 4:33 PM, Andy Pearson wrote: > Hey List, > > Hope all is well, I haven't been keeping up with the list whilst I > wasn't doing much Rails work but I'm back in the game and looking > forward to seeing how far things have progressed! > > I think I have probably mentioned this already on the list, but just > to reiterate: I am a beginner! > > I have installed the new Rspec and Cucumber for use with Rails, and > did what needed to be done to update libxml2 for use with nokogiri > and updated to the latest ZenTest but I am still seeing the > following stack trace when running `AUTOFEATURE=true autospec` > Warning: $KCODE is NONE. I have no idea about this one offhand. > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/ > step_definition.rb:100: warning: method redefined; discarding old > regexp > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/ > ruby_extensions/string.rb:31: warning: method redefined; discarding > old indent > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > node_classes/character_class.rb:13: warning: useless use of a > literal in void context > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler.rb:5: > warning: global variable `$exclude_metagrammar' not initialized > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:232: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:388: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:771: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:779: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:28: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:36: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:679: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:687: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:813: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:825: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/active_support/ > core_ext/blank.rb:49: warning: method redefined; discarding old blank? > The rest of these suggest that the same code is getting loaded twice, which could be due to using load instead of require or require statements that use relative paths instead of using expand_path or relying on the load path. > Is this something to be concerned about? The feature examples still > seem to run but I'm not sure whether I will get problems down the > line. > > Thanks for any help you can offer, I'd also run this by the cucumber group at http://groups.google.com/group/cukes . Cheers, David > > Andy > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Sat Jul 25 09:02:33 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 25 Jul 2009 08:02:33 -0500 Subject: [rspec-users] Errors when running AUTOFEATURE=true autospec [cucumber, autotest] In-Reply-To: References: <834053C3-F615-43B6-8720-99C2E061D98E@andy-pearson.com> <9C79530D-5D0B-47FF-AA77-869960A86283@googlemail.com> Message-ID: <57c63afe0907250602y248b2fdak2b3b32ee3f7d4c69@mail.gmail.com> On Sat, Jul 25, 2009 at 6:57 AM, Andy Pearson wrote: > Wanted to add some more information to this.... > > Since Cucumber seemed to be working, I created a feature and used it > to drive the initial creation of a new controller. > > Once the route was done I dropped down to a controller spec and wrote > the controller code that makes it pass. > > Now when I run AUTOFEATURE=true autospec it dies with the following > stack trace: > > Warning: $KCODE is NONE. > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/ > step_definition.rb:100: warning: method redefined; discarding old > regexp > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/ruby_extensions/ > string.rb:31: warning: method redefined; discarding old indent > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > node_classes/character_class.rb:13: warning: useless use of a literal > in void context > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler.rb:5: > warning: global variable `$exclude_metagrammar' not initialized > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:232: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:388: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:771: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/treetop-1.3.0/lib/treetop/compiler/ > metagrammar.rb:779: warning: method redefined; discarding old space > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:28: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:36: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:679: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:687: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:813: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/cucumber-0.3.90/lib/cucumber/parser/ > feature.rb:825: warning: method redefined; discarding old white > /Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/active_support/ > core_ext/blank.rb:49: warning: method redefined; discarding old blank? > /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby / > Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/bin/spec spec/ > controllers/lifestream_controller_spec.rb -O spec/spec.opts > /Library/Ruby/Gems/1.8/gems/rspec-1.2.8/lib/spec/autorun.rb:3: > undefined method `autorun' for Spec::Runner:Module (NoMethodError) > ? ? ? ?from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in > `gem_original_require' > ? ? ? ?from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in > `require' > ? ? ? ?from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/ > active_support/dependencies.rb:158:in `require' > ? ? ? ?from ./spec/controllers/../spec_helper.rb:5 > ? ? ? ?from ./spec/controllers/lifestream_controller_spec.rb:1:in `require' > ? ? ? ?from ./spec/controllers/lifestream_controller_spec.rb:1 > ? ? ? ?from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ > runner/example_group_runner.rb:15:in `load' > ? ? ? ?from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ > runner/example_group_runner.rb:15:in `load_files' > ? ? ? ?from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ > runner/example_group_runner.rb:14:in `each' > ? ? ? ?from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ > runner/example_group_runner.rb:14:in `load_files' > ? ? ? ?from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ > runner/options.rb:94:in `run_examples' > ? ? ? ?from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/lib/spec/ > runner/command_line.rb:9:in `run' > ? ? ? ?from /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12/bin/spec:4 Looks like two versions of rspec are getting loaded: /Library/Ruby/Gems/1.8/gems/rspec-1.2.8 /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12 Do you need rspec-1.1.12 for some reason? I'm not sure why it's getting loaded, but I'm quite confident it won't get loaded if it's not there :) > > Odd thing is, rake spec gets me what I want: That's because the spec task is only loading one version of rspec. If you can find what's requiring rspec-1.1.12 you'll find the culprit. > > ... > Finished in 0.071358 seconds > 3 examples, 0 failures > > Based on this, my assumption is this is something to do with ZenTest, > have I got an incompatible set of gems? > > I tried to pull out the most relevant ones: > > aslakhellesoy-webrat (0.3.2.1) > autotest-rails (4.1.0) > cucumber (0.3.90, 0.1.16, 0.1.15) > nokogiri (1.3.2, 1.1.0) > rails (2.3.3, 2.2.2, 2.2.0, 2.1.2, 2.1.1, 2.1.0, 2.0.991, 2.0.2, > 2.0.1, 1.2.6, 1.2.3) > rspec (1.2.8, 1.1.12, 1.1.11) > rspec-rails (1.2.7.1, 1.1.12, 1.1.11) > treetop (1.3.0, 1.2.4) > webrat (0.4.4, 0.4.0, 0.3.4, 0.2.0) > ZenTest (4.1.3, 3.11.0, 3.10.0, 3.9.3, 3.9.2, 3.8.0, 3.6.1) > > Really hoping I can get some help with this, hitting a block like this > gets super frustrating, particularly what I am trying to "do the right > thing" by getting this up and running to I can BDD and hopefully write > better software! > > Thanks again in advance, > > Andy > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From rick.denatale at gmail.com Sat Jul 25 11:07:54 2009 From: rick.denatale at gmail.com (Rick DeNatale) Date: Sat, 25 Jul 2009 11:07:54 -0400 Subject: [rspec-users] basic noob question In-Reply-To: <2eb450d4-b4c4-4b45-a5aa-8819990ac389@l5g2000pra.googlegroups.com> References: <2eb450d4-b4c4-4b45-a5aa-8819990ac389@l5g2000pra.googlegroups.com> Message-ID: On Fri, Jul 24, 2009 at 9:40 PM, norm wrote: > I'm struggling mightily! > > I have a controller called Govtpositions, and I'm just trying to test > the standard Rails 'create' method. So it seems as if three examples > would be appropriate... one to test the instantiation of the new model > from the params hash, another to test that it was saved, and then the > final redirection. > > So just to test the model instantiation, I have: > > describe GovtpositionsController, "POST govtpositions to create govt > new government position" do > ?before do > ? ?Govtposition.stub!(:new).and_return(@govtposition) > ?end > > ?it "should create a new govt position model" do > ? ?@govtposition = mock_model(Govtposition) > ? ?@params = {"name"=>"chief"} > ? ?Govtposition.should_receive(:new).with(@params).and_return > (@govtposition) > ?end > end > > But this example fails due to "Mock 'Govtposition_1001' received > unexpected message :save with (no args)" > > Surely I should be able to test the save in a separate example, no? I > was expecting to declare just one expectation per example, with a > total of three for the create method. But the test failure seems to > suggest otherwise. > > Any clarification you can offer would be much appreciated. Well the controller code doesn't know it's under test so it will call save. You need to stub out save, probably in the before block, to return true. Then in the example which expects the save, set a message expectation. I'm also not sure how your current code is actually working, since, unless I'm mistaken, the before block runs before the example, and therefore before you assign a (new?) value to @govtposition, so in the before block you are stubbing whatever @govtposition refers to at that point, probably nil. But I also notice that you are never actually calling anything after the message expectation, so I suspect that this is a poorly abstracted version of your actual code. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale From celoserpa at gmail.com Mon Jul 27 12:39:58 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Mon, 27 Jul 2009 11:39:58 -0500 Subject: [rspec-users] How should you make sure the user is not authenticated Message-ID: <1e5bcefd0907270939o9037d74k338972d300030499@mail.gmail.com> Hey list! Let's say I have a user story for authentication/login. I have seen many cucumber scenarios like this: Scenario: User logs in succesfully Given I am not authenticated When I go to the login page ... >From what I have seen, the Given steps that check that the user is not authenticated, simply post to a logout action. I have seen others, such as directly checking the session for the user id, even though I IMHO it is not best practice, since the Given steps are for setting state for the rest of the steps (right?), and this would simply check for something, and the test would brake if so. So, it is not a matter of "checking the user is not authenticated", but of "setting the ground and making sure the user is not authenticated", which of course, means loggin out the user on this given step to make sure it is really not-authenticated. The only thing I think could go wrong is that some log-out algorithms could fail if you try to logout when already logged-out. Anyway, just sharing my thoughts, would love to know what others in the list think about that, Marcelo. From sfeley at gmail.com Mon Jul 27 13:44:20 2009 From: sfeley at gmail.com (Stephen Eley) Date: Mon, 27 Jul 2009 13:44:20 -0400 Subject: [rspec-users] How should you make sure the user is not authenticated In-Reply-To: <1e5bcefd0907270939o9037d74k338972d300030499@mail.gmail.com> References: <1e5bcefd0907270939o9037d74k338972d300030499@mail.gmail.com> Message-ID: <1fb4df0907271044v67680fbdjdfab2f2ebfd8e4ef@mail.gmail.com> On Mon, Jul 27, 2009 at 12:39 PM, Marcelo de Moraes Serpa wrote: > > So, it is not a matter of "checking the user is not authenticated", > but of "setting the ground and making sure the user is not > authenticated", which of course, means loggin out the user on this > given step to make sure it is really not-authenticated. Yes, but POSTing to a logout action seems like overkill. If a Web request to a controller action is strictly required, your controllers are probably doing too much. The right answer depends on what "authentication" means in the context of your app. In most modern Rails authentication solutions there's a concept of a session, and logging out means getting rid of that session. I like AuthLogic because it's simple; the session itself is a model, and you can treat it as such: Given "I am not authenticated" do current_session.destroy if current_session end Tweak based on whatever methods/helpers/etc. you're using to track the current session. In restful_authentication, there's a logout_killing_session! method in the library file, which wraps some loose code to forget cookies and such. -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org From matt at mattwynne.net Tue Jul 28 04:38:20 2009 From: matt at mattwynne.net (Matt Wynne) Date: Tue, 28 Jul 2009 09:38:20 +0100 Subject: [rspec-users] How should you make sure the user is not authenticated In-Reply-To: <1e5bcefd0907270939o9037d74k338972d300030499@mail.gmail.com> References: <1e5bcefd0907270939o9037d74k338972d300030499@mail.gmail.com> Message-ID: <67F44587-BF8A-4F36-BEE2-140CE470F4DE@mattwynne.net> On 27 Jul 2009, at 17:39, Marcelo de Moraes Serpa wrote: > Hey list! > > Let's say I have a user story for authentication/login. > > I have seen many cucumber scenarios like this: > > Scenario: User logs in succesfully > > Given I am not authenticated > When I go to the login page > ... > >> From what I have seen, the Given steps that check that the user is >> not > authenticated, simply post to a logout action. I have seen others, > such as directly checking the session for the user id, even though I > IMHO it is not best practice, since the Given steps are for setting > state for the rest of the steps (right?), and this would simply check > for something, and the test would brake if so. > > So, it is not a matter of "checking the user is not authenticated", > but of "setting the ground and making sure the user is not > authenticated", which of course, means loggin out the user on this > given step to make sure it is really not-authenticated. The only thing > I think could go wrong is that some log-out algorithms could fail if > you try to logout when already logged-out. > > Anyway, just sharing my thoughts, would love to know what others in > the list think about that, My equivalent step is implemented like this: Given /I am not logged in/ do # of course you're not logged in! end Don't forget the session is thrown away for each scenario, so if you're starting a new scenario you wont' be logged in, by default. cheers, Matt +447974 430184 matt at mattwynne.net http://mattwynne.net From celoserpa at gmail.com Tue Jul 28 11:02:54 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Tue, 28 Jul 2009 10:02:54 -0500 Subject: [rspec-users] How should you make sure the user is not authenticated In-Reply-To: <67F44587-BF8A-4F36-BEE2-140CE470F4DE@mattwynne.net> References: <1e5bcefd0907270939o9037d74k338972d300030499@mail.gmail.com> <67F44587-BF8A-4F36-BEE2-140CE470F4DE@mattwynne.net> Message-ID: <1e5bcefd0907280802p6bc609c4m49949dc6410b7f71@mail.gmail.com> >My equivalent step is implemented like this: > >Given /I am not logged in/ do > # of course you're not logged in! >end > > >Don't forget the session is thrown away for each scenario, so if you're starting a new scenario you wont' be logged in, by default. > >cheers, >Matt Thank you all for the replies! @Stephen: Thanks for the tip, this seems like a good approach. @Matt: Unless you have a a background scenario that logs in the user for whatever reason :) But yeah, I was thinking to do something like this. Thanks, Marcelo. On Tue, Jul 28, 2009 at 3:38 AM, Matt Wynne wrote: > > On 27 Jul 2009, at 17:39, Marcelo de Moraes Serpa wrote: > >> Hey list! >> >> Let's say I have a user story for authentication/login. >> >> I have seen many cucumber scenarios like this: >> >> Scenario: User logs in succesfully >> >> Given I am not authenticated >> When I go to the login page >> ... >> >>> From what I have seen, the Given steps that check that the user is not >> >> authenticated, simply post to a logout action. I have seen others, >> such as directly checking the session for the user id, even though I >> IMHO it is not best practice, since the Given steps are for setting >> state for the rest of the steps (right?), and this would simply check >> for something, and the test would brake if so. >> >> So, it is not a matter of "checking the user is not authenticated", >> but of "setting the ground and making sure the user is not >> authenticated", which of course, means loggin out the user on this >> given step to make sure it is really not-authenticated. The only thing >> I think could go wrong is that some log-out algorithms could fail if >> you try to logout when already logged-out. >> >> Anyway, just sharing my thoughts, would love to know what others in >> the list think about that, > > My equivalent step is implemented like this: > > Given /I am not logged in/ do > ?# of course you're not logged in! > end > > > Don't forget the session is thrown away for each scenario, so if you're > starting a new scenario you wont' be logged in, by default. > > cheers, > Matt > > +447974 430184 > matt at mattwynne.net > http://mattwynne.net > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From codehacker at comcast.net Tue Jul 28 13:44:37 2009 From: codehacker at comcast.net (norm) Date: Tue, 28 Jul 2009 10:44:37 -0700 (PDT) Subject: [rspec-users] passing parameters when testing controller actions Message-ID: <0435a8dc-3742-4fcc-885a-47f7c10009f1@d15g2000prc.googlegroups.com> My grad_surveys_controller has a student_edit method. An id parameter is passed in the url and a token is passed in a query string. For example: http://www.example.com/students/34/grad_surveys/21/student_edit?token=ab56e1b47 so the params hash should contain {"controller"=>"grad_surveys","action"=>"student_edit", "student_id"=>"34", "id"=>"21", "token"=>"ab56e1b47"} I am trying to test using the following example: it "should permit student access with the correct token supplied" do GradSurvey.stub!(:accepts_as_authorized).with (:id=>"1",:token=>"ab56e1b47").and_return(true) GradSurvey.should_receive(:accepts_as_authorized).with (:id=>"1", :token=>"ab56e1b47") get :student_edit, {:id=>"1", :token=>"ab56e1b47"} end which fails with the message: expected :accepts_as_authorized with ({:token=>"ab56e1b47", :id=>"1"}) but received it with ("1", "ab56e1b47") What is the error in my example syntax that is causing the parameters not to be correctly recognized. thanks in advance for any assistance you are able to providd From celoserpa at gmail.com Tue Jul 28 14:28:22 2009 From: celoserpa at gmail.com (Marcelo de Moraes Serpa) Date: Tue, 28 Jul 2009 13:28:22 -0500 Subject: [rspec-users] Telling rspec that a method might be called with a OR b ... ? Message-ID: <1e5bcefd0907281128n6c7fb480p2be8d268bed17ff8@mail.gmail.com> Hey list, Let's say I have a model, and I want to setup a partial mock in it, to set the expectation that it should receive a message with specific arguments. I know that this is not a good design for a spec, and that I would probably need to separate this in another example -- but let's say that I would like to express in the example that this partial mock could receive this message with agument a OR b. I had a situation where I have setup the should receive with a specific argument, but later on in the code flow, the class received the same message (find) with other arguments, which was breaking the spec. The null_object pattern would not work, because afaik, it is only for messages and not for arguments. Any way to tell rspec that a method might be called with argument A or B (or C) ? :) Thanks in advance, MArcelo. From r_j_h_box-sf at yahoo.com Tue Jul 28 15:18:30 2009 From: r_j_h_box-sf at yahoo.com (r_j_h_box-sf at yahoo.com) Date: Tue, 28 Jul 2009 12:18:30 -0700 (PDT) Subject: [rspec-users] How should you make sure the user is not authenticated In-Reply-To: <67F44587-BF8A-4F36-BEE2-140CE470F4DE@mattwynne.net> References: <1e5bcefd0907270939o9037d74k338972d300030499@mail.gmail.com> <67F44587-BF8A-4F36-BEE2-140CE470F4DE@mattwynne.net> Message-ID: <274777.32876.qm@web31809.mail.mud.yahoo.com> ----- Original Message ---- > From: Matt Wynne > Don't forget the session is thrown away for each scenario, so if you're starting > a new scenario you wont' be logged in, by default. I wonder if that is true also when Webrat/Selenium or Celerity is involved? Randy From dchelimsky at gmail.com Tue Jul 28 16:57:56 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 28 Jul 2009 16:57:56 -0400 Subject: [rspec-users] Telling rspec that a method might be called with a OR b ... ? In-Reply-To: <1e5bcefd0907281128n6c7fb480p2be8d268bed17ff8@mail.gmail.com> References: <1e5bcefd0907281128n6c7fb480p2be8d268bed17ff8@mail.gmail.com> Message-ID: <57c63afe0907281357n3b76d23eic87362a4f0b3d2f9@mail.gmail.com> On Tue, Jul 28, 2009 at 2:28 PM, Marcelo de Moraes Serpa wrote: > Hey list, > > Let's say I have a model, and I want to setup a partial mock in it, to > set the expectation that it should receive a message with specific > arguments. I know that this is not a good design for a spec, and that > I would probably need to separate this in another example -- but let's > say that I would like to express in the example that this partial mock > could receive this message with agument a OR b. I had a situation > where I have setup the should receive with a specific argument, but > later on in the code flow, the class received the same message (find) > with other arguments, which was breaking the spec. The null_object > pattern would not work, because afaik, it is only for messages and not > for arguments. Any way to tell rspec that a method might be called > with argument A or B (or C) ? :) There is no way to specify A or B, but you can stub both and set expectations about the resulting behavior that tie back to the stub values you set up. For example: @thing = stub_model(Thing) Thing.stub!(:new).and_return(@thing) Thing.stub!(:create).and_return(@thing) Thing.stub!(:create!).and_return(@thing) If you do that in a before(:each) block, now the implementation can use any of those methods and set expectations like assigns[:thing].should == @thing, etc. This effectively does what you are doing, but not in the most explicit way. HTH, David > > Thanks in advance, > > MArcelo. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From hayafirst at gmail.com Tue Jul 28 16:59:06 2009 From: hayafirst at gmail.com (Yi Wen) Date: Tue, 28 Jul 2009 15:59:06 -0500 Subject: [rspec-users] [cucumber] table.hashes Message-ID: <23E450FC-AAA9-4570-88E6-7FF01982550F@gmail.com> Hi, I had a couple features failed with cucumber 0.3.90 because table.hashes is frozen so I cannot change the hashes anymore. Just wonder what's the reason behind the change? Thanks Yi From ben at benmabey.com Tue Jul 28 17:34:53 2009 From: ben at benmabey.com (Ben Mabey) Date: Tue, 28 Jul 2009 15:34:53 -0600 Subject: [rspec-users] [cucumber] table.hashes In-Reply-To: <23E450FC-AAA9-4570-88E6-7FF01982550F@gmail.com> References: <23E450FC-AAA9-4570-88E6-7FF01982550F@gmail.com> Message-ID: <4A6F6EFD.10507@benmabey.com> Yi Wen wrote: > Hi, > > I had a couple features failed with cucumber 0.3.90 because > table.hashes is frozen so I cannot change the hashes anymore. Just > wonder what's the reason behind the change? Thanks > > Yi > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users This change was added to avoid unintended side effects... I can't remember the exact use case though. If you need to modify it call #dup on the hashes first. Also, please use Cucumber's mailing list in the future: http://groups.google.com/group/cukes Thanks, Ben From andy.james.pearson at googlemail.com Tue Jul 28 17:09:30 2009 From: andy.james.pearson at googlemail.com (Andy Pearson) Date: Tue, 28 Jul 2009 14:09:30 -0700 (PDT) Subject: [rspec-users] Errors when running AUTOFEATURE=true autospec [cucumber, autotest] In-Reply-To: <57c63afe0907250602y248b2fdak2b3b32ee3f7d4c69@mail.gmail.com> References: <834053C3-F615-43B6-8720-99C2E061D98E@andy-pearson.com> <9C79530D-5D0B-47FF-AA77-869960A86283@googlemail.com> <57c63afe0907250602y248b2fdak2b3b32ee3f7d4c69@mail.gmail.com> Message-ID: <06361f60-5cd2-4191-9626-8d5e14646075@w41g2000yqb.googlegroups.com> Hey David, > Looks like two versions of rspec are getting loaded: > > /Library/Ruby/Gems/1.8/gems/rspec-1.2.8 > /Users/andypearson/.gem/ruby/1.8/gems/rspec-1.1.12 > > Do you need rspec-1.1.12 for some reason? I'm not sure why it's > getting loaded, but I'm quite confident it won't get loaded if it's > not there :) Yup, that seemed to be the problem, I removed respec-1.1.12 from my home directory (I'm guessing I tried to install rspec without `sudo` at some point) and now my specs are running fine. Still having the problem with cucumber but I will post on that list and see if anyone there can help me, sorry to clutter up this list - last time I was here there was a discussion to see whether the lists should be split or not, looks like you all came to a decision :) Thanks for your time, Andy From dchelimsky at gmail.com Tue Jul 28 17:38:35 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 28 Jul 2009 17:38:35 -0400 Subject: [rspec-users] [cucumber] table.hashes In-Reply-To: <23E450FC-AAA9-4570-88E6-7FF01982550F@gmail.com> References: <23E450FC-AAA9-4570-88E6-7FF01982550F@gmail.com> Message-ID: On Jul 28, 2009, at 4:59 PM, Yi Wen wrote: > Hi, > > I had a couple features failed with cucumber 0.3.90 because > table.hashes is frozen so I cannot change the hashes anymore. Just > wonder what's the reason behind the change? Thanks > > Yi Please address cucumber questions to the cukes google group: http://groups.google.com/group/cukes Cheers, David > From rick.denatale at gmail.com Tue Jul 28 17:38:32 2009 From: rick.denatale at gmail.com (Rick DeNatale) Date: Tue, 28 Jul 2009 17:38:32 -0400 Subject: [rspec-users] Telling rspec that a method might be called with a OR b ... ? In-Reply-To: <57c63afe0907281357n3b76d23eic87362a4f0b3d2f9@mail.gmail.com> References: <1e5bcefd0907281128n6c7fb480p2be8d268bed17ff8@mail.gmail.com> <57c63afe0907281357n3b76d23eic87362a4f0b3d2f9@mail.gmail.com> Message-ID: On Tue, Jul 28, 2009 at 4:57 PM, David Chelimsky wrote: >> for arguments. Any way to tell rspec that a method might be called >> with argument A or B (or C) ? :) > > There is no way to specify A or B, but you can stub both and set > expectations about the resulting behavior that tie back to the stub > values you set up. For example: > > @thing = stub_model(Thing) > Thing.stub!(:new).and_return(@thing) > Thing.stub!(:create).and_return(@thing) > Thing.stub!(:create!).and_return(@thing) > > If you do that in a before(:each) block, now the implementation can > use any of those methods and set expectations like > assigns[:thing].should == @thing, etc. This effectively does what you > are doing, but not in the most explicit way. I think though that the OP want's to set a message expectation with variation on the arguments, NOT which message. Something like @some_object.should_receive(:some_message).with(any_of(a, b, c)) As long as you don't need to set different return values for different arguments, that could be done with a new ArgumentMatcher. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale From rogerpack2005 at gmail.com Tue Jul 28 20:03:24 2009 From: rogerpack2005 at gmail.com (rogerdpack) Date: Tue, 28 Jul 2009 17:03:24 -0700 (PDT) Subject: [rspec-users] error message feedback Message-ID: Question. Currently with this test: instance.splatted3().should raise_error(ArgumentError) if it's broken, and returns [] instead of raising appropriately, currently the error reported is: expected ArgumentError, got # shouldn't this have been reported as expected ArgumentError, got [] ? Much thanks. =r From dchelimsky at gmail.com Wed Jul 29 08:10:30 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 29 Jul 2009 08:10:30 -0400 Subject: [rspec-users] passing parameters when testing controller actions In-Reply-To: <0435a8dc-3742-4fcc-885a-47f7c10009f1@d15g2000prc.googlegroups.com> References: <0435a8dc-3742-4fcc-885a-47f7c10009f1@d15g2000prc.googlegroups.com> Message-ID: <57c63afe0907290510s158eb5d8o383154307d08a396@mail.gmail.com> On Tue, Jul 28, 2009 at 1:44 PM, norm wrote: > My grad_surveys_controller has a student_edit method. An id parameter > is passed in the url and a token is passed in a query string. For > example: > ? ? ? ? ?http://www.example.com/students/34/grad_surveys/21/student_edit?token=ab56e1b47 > so the params hash should contain > {"controller"=>"grad_surveys","action"=>"student_edit", > "student_id"=>"34", "id"=>"21", "token"=>"ab56e1b47"} > > I am trying to test using the following example: > > ?it "should permit student access with the correct token supplied" do > ? ?GradSurvey.stub!(:accepts_as_authorized).with > (:id=>"1",:token=>"ab56e1b47").and_return(true) > ? ?GradSurvey.should_receive(:accepts_as_authorized).with > (:id=>"1", :token=>"ab56e1b47") Why are you using both stub!() and should_receive() here? The should_receive() call essentially overrides stub!(), so stub!() has no effect in this example. > ? ?get :student_edit, {:id=>"1", :token=>"ab56e1b47"} > ?end > > which fails with the message: > > expected :accepts_as_authorized with ({:token=>"ab56e1b47", :id=>"1"}) > but received it with ("1", "ab56e1b47") > > What is the error in my example syntax that is causing the parameters > not to be correctly recognized. > > thanks in advance for any assistance you are able to provide The error says that it's expecting {:token=>"ab56e1b47", :id=>"1"}, which looks correct based on the line that's setting up the expectation. So I think the problem may be with the code, not the spec. Can you post the controller code? From dchelimsky at gmail.com Wed Jul 29 08:12:36 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 29 Jul 2009 08:12:36 -0400 Subject: [rspec-users] error message feedback In-Reply-To: References: Message-ID: <57c63afe0907290512g6487bb5bn306b624268364bf2@mail.gmail.com> On Tue, Jul 28, 2009 at 8:03 PM, rogerdpack wrote: > Question. > > Currently with this test: > > ?instance.splatted3().should raise_error(ArgumentError) This should be: lambda { instance.splatted3() }.should raise_error(ArgumentError) See http://rspec.rubyforge.org/rspec/1.2.8/classes/Spec/Matchers.html#M000450 for more info. HTH, David > > if it's broken, and returns [] instead of raising appropriately, > currently the error reported is: > > expected ArgumentError, got # for []:Array> > > shouldn't this have been reported as > > expected ArgumentError, got [] > > ? > Much thanks. > =r > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From codehacker at comcast.net Wed Jul 29 08:51:53 2009 From: codehacker at comcast.net (norm) Date: Wed, 29 Jul 2009 05:51:53 -0700 (PDT) Subject: [rspec-users] passing parameters when testing controller actions In-Reply-To: <57c63afe0907290510s158eb5d8o383154307d08a396@mail.gmail.com> References: <0435a8dc-3742-4fcc-885a-47f7c10009f1@d15g2000prc.googlegroups.com> <57c63afe0907290510s158eb5d8o383154307d08a396@mail.gmail.com> Message-ID: <73f8c98d-7fd7-4191-841b-6cc22f6705a3@p10g2000prm.googlegroups.com> wow, thanks for the tips David. I'm using both the stub!() and the should_receive() because, as a complete RSpec neophyte, that's what I thought was required! (And it seemed to work, too!). I didn't suspect the controller code, because it's code that works. But with your suggestion, I found the discrepancy between the controller code and the spec... the error message was telling me but I wasn't seeing it! From dchelimsky at gmail.com Wed Jul 29 08:53:36 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 29 Jul 2009 08:53:36 -0400 Subject: [rspec-users] passing parameters when testing controller actions In-Reply-To: <73f8c98d-7fd7-4191-841b-6cc22f6705a3@p10g2000prm.googlegroups.com> References: <0435a8dc-3742-4fcc-885a-47f7c10009f1@d15g2000prc.googlegroups.com> <57c63afe0907290510s158eb5d8o383154307d08a396@mail.gmail.com> <73f8c98d-7fd7-4191-841b-6cc22f6705a3@p10g2000prm.googlegroups.com> Message-ID: <57c63afe0907290553s1b3e963cq3c7ab645926b7b92@mail.gmail.com> On Wed, Jul 29, 2009 at 8:51 AM, norm wrote: > wow, thanks for the tips David. I'm using both the stub!() and the > should_receive() because, as a complete RSpec neophyte, that's what I > thought was required! (And it seemed to work, too!). > > I didn't suspect the controller code, because it's code that works. > But with your suggestion, I found the discrepancy between the > controller code and the spec... the error message was telling me but I > wasn't seeing it! Glad to hear you've got it sorted out. Cheers, David From andy.james.pearson at googlemail.com Thu Jul 30 17:56:03 2009 From: andy.james.pearson at googlemail.com (Andy Pearson) Date: Thu, 30 Jul 2009 14:56:03 -0700 (PDT) Subject: [rspec-users] [rspec, rails] A quick code review, to help a beginner Message-ID: Hey List, This is probably a bit cheeky, but I thought it may be worth a try. I have just started a new rails app, which I am going to try and drive with Rspec and Cucumber as best as I can. I have just finished a couple of tests, and a couple of lines of a Model and I was wondering whether I could get a bit of feedback on what I have written so far, before I go down to totally wrong route. - How are my specs? - Am I testing the right things? - could things be done better? - Am I being too broad, or too granular? I have created a Gist at of the spec and the resulting code at: http://gist.github.com/158954 Kind of off topic but, is there a "more Ruby" way of doing the self.download method? It feels very PHP (my native language) - setting an empty Array and then filling it up. I realise this is very focused on just my code, so if this is too much to ask, then please let me know and I will move along :) Andy From codehacker at comcast.net Thu Jul 30 19:13:30 2009 From: codehacker at comcast.net (norm) Date: Thu, 30 Jul 2009 16:13:30 -0700 (PDT) Subject: [rspec-users] stubbing association methods in controller spec Message-ID: <1e82ef7d-557d-47d1-864f-b4a8aff90cad@y28g2000prd.googlegroups.com> I have two models: Student has_many=>:grad_surveys and GradSurvey belongs_to=>:student I am testing the GradSurveysController#update method with it "should only allow update if the correct token is presented" do @params = {:id=>"1", :token=>"ab56e1b47", :survey=>{}} @mock_grad_survey = mock_model(GradSurvey) GradSurvey.should_receive(:accepts_as_authorized).with(@params [:id], at params[:token]).and_return(@mock_grad_survey) put :update, @params response.should render_template(:update) end Inside the GradSurvey#accepts_as_authorized method is called the GradSurvey#student method. The test above fails with the message: Mock 'GradSurvey_1034' received unexpected message :student with (no args) However I can make the test pass by adding the :student method to the @mock_grad_survey object, so @mock_grad_survey = mock_model(GradSurvey, :student=>mock_model (Student)) Why is it necessary for me to explicitly add the #student method to the mock GradSurvey model, it seems to me that it should come for free as part of the defined association, no? What am I not understanding here? thanks in advance for any light you are able to shed From lists at ruby-forum.com Fri Jul 31 01:00:41 2009 From: lists at ruby-forum.com (Adrian De la cruz) Date: Fri, 31 Jul 2009 07:00:41 +0200 Subject: [rspec-users] Ruby 1.9 - autospec loadpath problem In-Reply-To: <57c63afe0902190849m4954d192iac667c318a10038@mail.gmail.com> References: <57a815bf0811301424k131b4155xafee88459449eb@mail.gmail.com> <57c63afe0812010221x58dd37e3w1a7d741df1256b70@mail.gmail.com> <22102148.post@talk.nabble.com> <57c63afe0902190849m4954d192iac667c318a10038@mail.gmail.com> Message-ID: <35c4e29e0d69b2ad8289417c003e3506@ruby-forum.com> Hi guys, I'm getting this same error in a Ubuntu 9.04 machine. I have Ruby 1.8.7 installed alongside Ruby 1.9.1. When I'm on Ruby 1.9.1 and try to run the specs on a non-Rails app I get this error: ======================================= /usr/local/bin/spec:19:in `load': Is a directory - /usr/local/lib/ruby19/gems/1.9.1/gems/rspec-rails-1.2.7.1/lib/spec (Errno::EISDIR) from /usr/local/bin/spec:19:in `
' ======================================= If I switch to Ruby 1.8.7 there is no problem. Any ideas? -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Fri Jul 31 01:03:38 2009 From: lists at ruby-forum.com (Adrian De la cruz) Date: Fri, 31 Jul 2009 07:03:38 +0200 Subject: [rspec-users] Ruby 1.9 - autospec loadpath problem In-Reply-To: <35c4e29e0d69b2ad8289417c003e3506@ruby-forum.com> References: <57a815bf0811301424k131b4155xafee88459449eb@mail.gmail.com> <57c63afe0812010221x58dd37e3w1a7d741df1256b70@mail.gmail.com> <22102148.post@talk.nabble.com> <57c63afe0902190849m4954d192iac667c318a10038@mail.gmail.com> <35c4e29e0d69b2ad8289417c003e3506@ruby-forum.com> Message-ID: <8b1fb7bcb087f0690627a0bb37898a1a@ruby-forum.com> Oh, forgot to mention that if I uninstall the rspec-rails gem from my Ruby 1.9.1 gem repository, the problem goes away, and I can run the tests in the non-Rails app without problem. -- Posted via http://www.ruby-forum.com/. From brian.takita at gmail.com Fri Jul 31 03:11:29 2009 From: brian.takita at gmail.com (Brian Takita) Date: Fri, 31 Jul 2009 00:11:29 -0700 Subject: [rspec-users] Rspec api ideas, inspired by Cucumber Message-ID: <1d7ddd110907310011u146314c3k3be016b63f37786c@mail.gmail.com> I think I would like to stick primarily with Rspec, because I like working close to the code and being able to easily refactor things. I do like Cucumber's ability to easily define sequences of actions and custom plain text actions. Here are some api ideas I have to make those things better in rspec. http://gist.github.com/159123 WDYT? - Brian From dchelimsky at gmail.com Fri Jul 31 07:36:45 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 31 Jul 2009 06:36:45 -0500 Subject: [rspec-users] Rspec api ideas, inspired by Cucumber In-Reply-To: <1d7ddd110907310011u146314c3k3be016b63f37786c@mail.gmail.com> References: <1d7ddd110907310011u146314c3k3be016b63f37786c@mail.gmail.com> Message-ID: <57c63afe0907310436paf00d0ex80bd60e7c07b502f@mail.gmail.com> On Fri, Jul 31, 2009 at 2:11 AM, Brian Takita wrote: > I think I would like to stick primarily with Rspec, because I like > working close to the code and being able to easily refactor things. > I do like Cucumber's ability to easily define sequences of actions and > custom plain text actions. > > Here are some api ideas I have to make those things better in rspec. > http://gist.github.com/159123 > > WDYT? Very cool ideas. I'm not sure whether I'd want them in rspec proper, but I do think that rspec should let you easily plug ideas like this into its example lifecycle management. That idea has been brewing for a while as part of rspec-2.0, but rspec-2.0 is back-burnered until after the book is off to print - I need to stay focused. What you're proposing looks a lot like rbehave, which was the precursor to Story Runner, and then Cucumber. Story Runner introduced plain text scenarios, but still let you work in Ruby (like rbehave) if you chose to. In rbehave, your example would look like http://gist.github.com/159174 FIT's DoFixture does the same sort of alternating part of the method with arguments. So you can write table rows like this: | can have | any | number of | arguments | between text | .. which get converted to method calls like this (pardon the camel case): canHaveNumberOfBetweenText('any','arguments') This actually dates back to smalltalk, which lets you make method calls with something akin to keyword arguments like this: message canHave: 'any'; numberOf: 'arguments' It's more fluid than rbehave's name/arg structure, in which all of the args have to be at the end, and I think its a powerful idea. The lambda in the middle is a bit disconcerting. Maybe that bit would look nicer in Ruby 1.9 :) Other thoughts? Cheers, David > > - Brian > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From balint.erdi at gmail.com Fri Jul 31 04:57:37 2009 From: balint.erdi at gmail.com (Balint Erdi) Date: Fri, 31 Jul 2009 01:57:37 -0700 (PDT) Subject: [rspec-users] [rspec-rails] rspec-rails forcing test environment? Message-ID: Hi, I realized that requiring rspec-rails somehow sets RAILS_ENV to 'test'. My experimental environment.rb file is the following: puts "XXX RAILS_ENV: #{RAILS_ENV}" Rails::Initializer.run do |config| (...) config.gem "rspec", :version => ">= 1.2.8", :lib => 'spec' config.gem "rspec-rails", :version => ">= 1.2.7.1", :lib => 'spec/ rails' # (1) (...) end puts "XXX RAILS_ENV: #{RAILS_ENV}" When I run the server (./script/server), I see: XXX RAILS_ENV: development XXX RAILS_ENV: test If I comment out the rspec-rails inclusion line (1), then the RAILS_ENV remains 'development' after the config block. That's causing me a problem because "in the real app" I set up a CouchDB database depending on RAILS_ENV after the block. One possible workaround is to put the rspec and rspec-rails lines into the environment file for the test env. (test.rb), but that seems to defeat the goal of having all dependencies in one place so that it is clear and gem-related rake tasks can be conveniently run. (I am using rails 2.3.2, rspec 1.2.8, rspec-rails 1.2.7.1) Do you know of a better workaround or a real solution for the problem? Thank you, Balint From dchelimsky at gmail.com Fri Jul 31 08:36:39 2009 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 31 Jul 2009 07:36:39 -0500 Subject: [rspec-users] [rspec-rails] rspec-rails forcing test environment? In-Reply-To: References: Message-ID: <57c63afe0907310536o174ca777pb9732fb4e19ead7c@mail.gmail.com> On Fri, Jul 31, 2009 at 3:57 AM, Balint Erdi wrote: > Hi, > > I realized that requiring rspec-rails somehow sets RAILS_ENV to > 'test'. My experimental environment.rb file is the following: > > puts "XXX RAILS_ENV: #{RAILS_ENV}" > > Rails::Initializer.run do |config| > ?(...) > ?config.gem "rspec", :version => ">= 1.2.8", :lib => 'spec' > ?config.gem "rspec-rails", :version => ">= 1.2.7.1", :lib => 'spec/ Use lib => false to keep rails from automatically loading rspec and/or rspec-rails This should also be in config/environments/test.rb HTH, David > rails' # (1) > ?(...) > end > > puts "XXX RAILS_ENV: #{RAILS_ENV}" > > When I run the server (./script/server), I see: > > XXX RAILS_ENV: development > XXX RAILS_ENV: test > > If I comment out the rspec-rails inclusion line (1), then the > RAILS_ENV remains 'development' after the config block. That's causing > me a problem because "in the real app" I set up a CouchDB database > depending on RAILS_ENV after the block. > > One possible workaround is to put the rspec and rspec-rails lines into > the environment file for the test env. (test.rb), but that seems to > defeat the goal of having all dependencies in one place so that it is > clear and gem-related rake tasks can be conveniently run. (I am using > rails 2.3.2, rspec 1.2.8, rspec-rails 1.2.7.1) > > Do you know of a better workaround or a real solution for the problem? > > Thank you, > Balint > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >