From elc at mingins.com Mon Aug 1 22:29:28 2011 From: elc at mingins.com (Shane Mingins) Date: Tue, 2 Aug 2011 14:29:28 +1200 Subject: [rspec-users] Testing Routing Constraint => redirect example.com to www.example.com Message-ID: Hi All With the following route constraints(:host => "example.com") do match "(*x)" => redirect { |params, request| URI.parse(request.url).tap { |x| x.host = "www.example.com" }.to_s } end I am wondering how or where I would spec this? Ideally I would like to be able to write a routing spec, something like: get("http://example.com").should route_to(:controller => "home", :action => "index", :host => "www.example.com") Thanks in advance Shane Mingins -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Tue Aug 2 05:37:06 2011 From: jko170 at gmail.com (Justin Ko) Date: Tue, 2 Aug 2011 03:37:06 -0600 Subject: [rspec-users] Testing Routing Constraint => redirect example.com to www.example.com In-Reply-To: References: Message-ID: On Mon, Aug 1, 2011 at 8:29 PM, Shane Mingins wrote: > Hi All > > With the following route > > constraints(:host => "example.com") do > match "(*x)" => redirect { |params, request| > URI.parse(request.url).tap { |x| x.host = "www.example.com" }.to_s > } > end > > > I am wondering how or where I would spec this? > > Ideally I would like to be able to write a routing spec, something like: > > get("http://example.com").should route_to(:controller => "home", :action > => "index", :host => "www.example.com") > > Thanks in advance > > Shane Mingins > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Hi, take a look at this: https://github.com/rspec/rspec-rails/issues/416 -------------- next part -------------- An HTML attachment was scrubbed... URL: From piter.fcbk at gmail.com Tue Aug 2 16:41:40 2011 From: piter.fcbk at gmail.com (Piter Fcbk) Date: Tue, 2 Aug 2011 17:41:40 -0300 Subject: [rspec-users] Best way to test tasks Message-ID: I have a task that runs frequently in order to get/import data from another system. Because of this I wanted to know which is the best way to test tasks in order to create the tests needed. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mguterl at gmail.com Tue Aug 2 17:51:43 2011 From: mguterl at gmail.com (Michael Guterl) Date: Tue, 2 Aug 2011 17:51:43 -0400 Subject: [rspec-users] Best way to test tasks In-Reply-To: References: Message-ID: On Tue, Aug 2, 2011 at 4:41 PM, Piter Fcbk wrote: > I have a task that runs frequently in order to get/import data from another > system. > Because of this I wanted to know which is the best way to test tasks in > order to create the tests needed. > > Thanks in advance. > Assuming you are asking about rake tasks... I typically extract the body of the task into a method and then test that method. This typically leaves the rake task very thin and I do not worry about testing the task itself. Best, Michael Guterl From patmaddox at me.com Tue Aug 2 17:31:55 2011 From: patmaddox at me.com (Pat Maddox) Date: Tue, 02 Aug 2011 17:31:55 -0400 Subject: [rspec-users] Best way to test tasks In-Reply-To: References: Message-ID: On Aug 2, 2011, at 4:41 PM, Piter Fcbk wrote: > I have a task that runs frequently in order to get/import data from another system. > Because of this I wanted to know which is the best way to test tasks in order to create the tests needed. > > Thanks in advance. I write simple objects that implement the behavior I want. Then I write rake tasks that instantiate and use those objects. I don't write automated tests for the tasks because they are thin layers over my well-tested objects. Pat From piter.fcbk at gmail.com Tue Aug 2 19:52:30 2011 From: piter.fcbk at gmail.com (Piter Fcbk) Date: Tue, 2 Aug 2011 20:52:30 -0300 Subject: [rspec-users] Best way to test tasks In-Reply-To: References: Message-ID: You are right, I'm talking about rake task. So the idea is to make an Importer object, do the unit test for that object and then the task just calls the methods of the objects right? Thanks a lot for the help, really appreciate it. On Tue, Aug 2, 2011 at 6:31 PM, Pat Maddox wrote: > On Aug 2, 2011, at 4:41 PM, Piter Fcbk wrote: > > > I have a task that runs frequently in order to get/import data from > another system. > > Because of this I wanted to know which is the best way to test tasks in > order to create the tests needed. > > > > Thanks in advance. > > I write simple objects that implement the behavior I want. Then I write > rake tasks that instantiate and use those objects. I don't write automated > tests for the tasks because they are thin layers over my well-tested > objects. > > Pat > > _______________________________________________ > 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 elc at mingins.com Tue Aug 2 22:19:43 2011 From: elc at mingins.com (Shane Mingins) Date: Wed, 3 Aug 2011 14:19:43 +1200 Subject: [rspec-users] Testing Routing Constraint => redirect example.com to www.example.com In-Reply-To: References: Message-ID: Thanks for that Justin! I was not aware of request specs http://relishapp.com/rspec/rspec-rails/v/2-6/dir/request-specs/request-spec... could not find any reference to them in the rspec book (maybe I missed them). so adding spec/requests/routes_spec.rb with the following did the trick! require "spec_helper" describe "routes that redirect" do it "redirects http://example.com to http://www.example.com" do get "http://example.com" response.should redirect_to('http://www.example.com') end it "should not redirect https://secure.example.com" do get "https://secure.example.com" response.should_not be_redirect end end constraints(:host => /^example.com/) do match "(*x)" => redirect { |params, request| URI.parse(request.url).tap { |x| x.host = "www.example.com" }.to_s } end On 2 August 2011 21:37, Justin Ko wrote: > > > On Mon, Aug 1, 2011 at 8:29 PM, Shane Mingins wrote: > >> Hi All >> >> With the following route >> >> constraints(:host => "example.com") do >> match "(*x)" => redirect { |params, request| >> URI.parse(request.url).tap { |x| x.host = "www.example.com" }.to_s >> } >> end >> >> >> I am wondering how or where I would spec this? >> >> Ideally I would like to be able to write a routing spec, something like: >> >> get("http://example.com").should route_to(:controller => "home", :action >> => "index", :host => "www.example.com") >> >> Thanks in advance >> >> Shane Mingins >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > Hi, take a look at this: > https://github.com/rspec/rspec-rails/issues/416 > > > _______________________________________________ > 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 jko170 at gmail.com Wed Aug 3 01:04:40 2011 From: jko170 at gmail.com (Justin Ko) Date: Tue, 2 Aug 2011 23:04:40 -0600 Subject: [rspec-users] Best way to test tasks In-Reply-To: References: Message-ID: On Tue, Aug 2, 2011 at 5:52 PM, Piter Fcbk wrote: > You are right, I'm talking about rake task. > So the idea is to make an Importer object, do the unit test for that object > and then the task just calls the methods of the objects right? > > Thanks a lot for the help, really appreciate it. > > > On Tue, Aug 2, 2011 at 6:31 PM, Pat Maddox wrote: > >> On Aug 2, 2011, at 4:41 PM, Piter Fcbk wrote: >> >> > I have a task that runs frequently in order to get/import data from >> another system. >> > Because of this I wanted to know which is the best way to test tasks in >> order to create the tests needed. >> > >> > Thanks in advance. >> >> I write simple objects that implement the behavior I want. Then I write >> rake tasks that instantiate and use those objects. I don't write automated >> tests for the tasks because they are thin layers over my well-tested >> objects. >> >> Pat >> >> _______________________________________________ >> 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 namespace :importer do task :import do Importer.import end end describe Importer do describe '.import' do ... -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Wed Aug 3 01:01:09 2011 From: jko170 at gmail.com (Justin Ko) Date: Tue, 2 Aug 2011 23:01:09 -0600 Subject: [rspec-users] Testing Routing Constraint => redirect example.com to www.example.com In-Reply-To: References: Message-ID: On Tue, Aug 2, 2011 at 8:19 PM, Shane Mingins wrote: > Thanks for that Justin! > > I was not aware of request specs > http://relishapp.com/rspec/rspec-rails/v/2-6/dir/request-specs/request-spec... could not find any reference to them in the rspec book (maybe I missed > them). > > so adding spec/requests/routes_spec.rb with the following did the trick! > > > require "spec_helper" > > describe "routes that redirect" do > > it "redirects http://example.com to http://www.example.com" do > get "http://example.com" > response.should redirect_to('http://www.example.com') > end > > it "should not redirect https://secure.example.com" do > get "https://secure.example.com" > response.should_not be_redirect > end > > end > > > constraints(:host => /^example.com/) do > match "(*x)" => redirect { |params, request| > URI.parse(request.url).tap { |x| x.host = "www.example.com" }.to_s > } > end > > > > On 2 August 2011 21:37, Justin Ko wrote: > >> >> >> On Mon, Aug 1, 2011 at 8:29 PM, Shane Mingins wrote: >> >>> Hi All >>> >>> With the following route >>> >>> constraints(:host => "example.com") do >>> match "(*x)" => redirect { |params, request| >>> URI.parse(request.url).tap { |x| x.host = "www.example.com" }.to_s >>> } >>> end >>> >>> >>> I am wondering how or where I would spec this? >>> >>> Ideally I would like to be able to write a routing spec, something like: >>> >>> get("http://example.com").should route_to(:controller => "home", :action >>> => "index", :host => "www.example.com") >>> >>> Thanks in advance >>> >>> Shane Mingins >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> >> Hi, take a look at this: >> https://github.com/rspec/rspec-rails/issues/416 >> >> >> _______________________________________________ >> 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 Awesome, glad it worked out! -------------- next part -------------- An HTML attachment was scrubbed... URL: From anexiole at gmail.com Wed Aug 3 04:24:18 2011 From: anexiole at gmail.com (ct9a) Date: Wed, 3 Aug 2011 01:24:18 -0700 (PDT) Subject: [rspec-users] A bit confused with the use of assign() to test views Message-ID: <1b27793d-f687-4d6a-ab9c-2ed439bd62a7@s21g2000pre.googlegroups.com> hi guys, I'm trying to pick up Rspec to port an existing application in rails 2.3.8 to rails 3. I'm using the pragmatic "The Rspec book" (dec 2010) as a reference. read around the book and the rspec docs. 1) assign method - syntax: assign( , - what it means to me: run the codes in the block and assign the value evaluated to the symbol name. 2) let method syntax: let(method_name>) { ? } - will not run until it is called -used with before(), end() to set up usually instance variables -the contents in its block will be evaluated when called -here's what http://rdoc.info/gems/rspec/1.3.2/frames says when i looked up the source for let. ------ Documentation extract for "let" (start) ---------------------- def let(name, &block) define_method name do @assignments ||= {} @assignments[name] ||= instance_eval(&block) end end ------ Documentation extract for "let" (end) ---------------------- -to call the contents of the block, we will refer to it by the method name (which is a symbol) There's this example below (from the rspec book page 338) which I am a little confused with. ------------ Extract start ---------------------------- 1 require 'spec_helper' 2 3 describe "messages/new.html.erb" do 4 let(:message) do 5 mock_model("Message").as_new_record.as_null_object 6 end 7 8 before do 9 assign(:message, message) 10 end 11 12 it "renders a form to create a message" do 13 render 14 rendered.should have_selector("form", 15 :method => "post", 16 :action => messages_path 17 ) do |form| 18 form.should have_selector("input", :type => "submit") 19 end 20 end 21 22 it "renders a text field for the message title" do 23 message.stub(:title => "the title") 24 render 25 rendered.should have_selector("form") do |form| 26 form.should have_selector("input", 27 :type => "text", 28 :name => "message[title]", 29 :value => "the title" 30 ) 31 end 32 end 33 end ------------ Extract end ---------------------------- My question is, line 9 seems to imply that the content of :message (the mocked Message object) is being assigned to the 'message' object but as per what I have read in the Rspec book, the first argument in the call to assign() is actually the variable that is getting assigned with. Yet, the extract above still works when i run "rake spec". I'm a bit confused. Please help. Thanks From anexiole at gmail.com Wed Aug 3 04:50:27 2011 From: anexiole at gmail.com (ct9a) Date: Wed, 3 Aug 2011 01:50:27 -0700 (PDT) Subject: [rspec-users] A bit confused with the use of assign() to test views In-Reply-To: <1b27793d-f687-4d6a-ab9c-2ed439bd62a7@s21g2000pre.googlegroups.com> References: <1b27793d-f687-4d6a-ab9c-2ed439bd62a7@s21g2000pre.googlegroups.com> Message-ID: <949d393f-629a-4d4f-a5ea-d32b307ff06d@h25g2000prf.googlegroups.com> also, i have just read a little more in the rspec book. here's an extract: ------------- extract start --------------------- assign() View specs expose an assign method, which we use to provide data to the view. Modify the spec as follows: describe "messages/show.html.erb" do it "displays the text attribute of the message" do assign(:message, double("Message", :text => "Hello world!")) render rendered.should contain("Hello world!") end end The new first line of the example creates a test double, which stubs the text( ) method with a return value of ?Hello world!? and assigns it to an @message instance variable on the view. ------------- extract end --------------------- If :message (in the view spec) can correspond to @message variable (in the actual show.html.erb view), is this a Rspec convention/thing? Sorry, Im just trying to find more resources to read up on rspec but i'm having not much luck. Appreciate your thoughts. Thank you . From dchelimsky at gmail.com Wed Aug 3 06:46:33 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 3 Aug 2011 05:46:33 -0500 Subject: [rspec-users] A bit confused with the use of assign() to test views In-Reply-To: <949d393f-629a-4d4f-a5ea-d32b307ff06d@h25g2000prf.googlegroups.com> References: <1b27793d-f687-4d6a-ab9c-2ed439bd62a7@s21g2000pre.googlegroups.com> <949d393f-629a-4d4f-a5ea-d32b307ff06d@h25g2000prf.googlegroups.com> Message-ID: <69E36B0E-6302-4734-9C5A-8F8CE40A663C@gmail.com> On Aug 3, 2011, at 3:50 AM, ct9a wrote: > also, i have just read a little more in the rspec book. > > here's an extract: > > ------------- extract start --------------------- > assign() > View specs expose an assign method, which we use to provide data to > the view. Modify the spec as follows: > > describe "messages/show.html.erb" do > it "displays the text attribute of the message" do > assign(:message, double("Message", :text => "Hello world!")) > render > rendered.should contain("Hello world!") > end > end > > > The new first line of the example creates a test double, which stubs > the text( ) method with a return value of ?Hello world!? and assigns > it to an @message instance variable on the view. > > ------------- extract end --------------------- > > If :message (in the view spec) can correspond to @message variable (in > the actual show.html.erb view), is this a Rspec convention/thing? > > Sorry, Im just trying to find more resources to read up on rspec but > i'm having not much luck. Appreciate your thoughts. > > Thank you . Keep in mind that rspec-rails is a thin wrapper around the built-in Rails testing framework. The convention of relating a symbol in the spec to an instance variable in the view was established by Rails with the `assigns` method in functional tests (controller specs in rspec): thing = Factory(:thing) get :index assigns(:thing).should eq(thing) In the last line, `assigns(:thing)` refers to the `@thing` instance variable in the view. HTH, David From anexiole at gmail.com Wed Aug 3 07:10:16 2011 From: anexiole at gmail.com (Gordon Yeong) Date: Wed, 3 Aug 2011 21:10:16 +1000 Subject: [rspec-users] A bit confused with the use of assign() to test views In-Reply-To: <949d393f-629a-4d4f-a5ea-d32b307ff06d@h25g2000prf.googlegroups.com> References: <1b27793d-f687-4d6a-ab9c-2ed439bd62a7@s21g2000pre.googlegroups.com> <949d393f-629a-4d4f-a5ea-d32b307ff06d@h25g2000prf.googlegroups.com> Message-ID: Hello David Thanks for that. Doesn't assign have 2 arguments with the first being the variable to be assigned to and the second being the contents? On Aug 3, 2011 7:01 PM, "ct9a" wrote: > also, i have just read a little more in the rspec book. > > here's an extract: > > ------------- extract start --------------------- > assign() > View specs expose an assign method, which we use to provide data to > the view. Modify the spec as follows: > > describe "messages/show.html.erb" do > it "displays the text attribute of the message" do > assign(:message, double("Message", :text => "Hello world!")) > render > rendered.should contain("Hello world!") > end > end > > > The new first line of the example creates a test double, which stubs > the text( ) method with a return value of ?Hello world!? and assigns > it to an @message instance variable on the view. > > ------------- extract end --------------------- > > If :message (in the view spec) can correspond to @message variable (in > the actual show.html.erb view), is this a Rspec convention/thing? > > Sorry, Im just trying to find more resources to read up on rspec but > i'm having not much luck. Appreciate your thoughts. > > Thank you . > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > -- > You received this message because you are subscribed to the Google Groups "rspec" group. > To post to this group, send email to rspec at googlegroups.com. > To unsubscribe from this group, send email to rspec+unsubscribe at googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rspec?hl=en. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Wed Aug 3 07:37:14 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 3 Aug 2011 06:37:14 -0500 Subject: [rspec-users] A bit confused with the use of assign() to test views In-Reply-To: References: <1b27793d-f687-4d6a-ab9c-2ed439bd62a7@s21g2000pre.googlegroups.com> <949d393f-629a-4d4f-a5ea-d32b307ff06d@h25g2000prf.googlegroups.com> Message-ID: >> Keep in mind that rspec-rails is a thin wrapper around the built-in Rails testing framework. The convention of relating a symbol in the spec to an instance variable in the view was established by Rails with the `assigns` method in functional tests (controller specs in rspec): >> >> thing = Factory(:thing) >> get :index >> assigns(:thing).should eq(thing) >> >> In the last line, `assigns(:thing)` refers to the `@thing` instance variable in the view. >> >> HTH, >> David > > Hello David > Thanks for that. Doesn't assign have 2 arguments with the first being the variable to be assigned to and the second being the contents? Yes, `assign`, in view specs, has two arguments. `assigns` (plural), in controller specs (and provided by Rails) takes only one. `assign` is a setter for a single instance variable, `assigns` is a getter which keys into a map of all assigned instance variables. HTH, David PS - I had to move your post to the bottom and quote my own comments from the previous email to which you were responding. Please read http://idallen.com/topposting.html and make it easier for people to be helpful to you in a way that is understandable to everyone else. From klebervirgilio at gmail.com Tue Aug 2 12:50:49 2011 From: klebervirgilio at gmail.com (Kleber Correia) Date: Tue, 2 Aug 2011 09:50:49 -0700 (PDT) Subject: [rspec-users] Rspec teting API. Message-ID: <6927012.4651.1312303849778.JavaMail.geo-discussion-forums@yqbp37> Hi! Guys, I wanna test an API built in Rails 3.0.3. So, my question is... What's the better way to test my own api? I guess that a requests specs like these: describe 'POST /client' it 'works' do assert_difference post '/clients', :client => {...} end end can be appropriate... Someone just tested an API? I mean... simulate the requests to API. Tks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From piter.fcbk at gmail.com Wed Aug 3 12:08:42 2011 From: piter.fcbk at gmail.com (Piter Fcbk) Date: Wed, 3 Aug 2011 13:08:42 -0300 Subject: [rspec-users] How to test modules and custom validators Message-ID: I'm trying to have most of the important stuff tested and in order to do that I run in front of some stuff I'm not quite sure which is the best way to test them. At this time I am having question around how to test modules and the custom validators (the typical email format validator for example). To test a module the best I saw up to now was finded here: http://stackoverflow.com/questions/512466/how-to-implement-an-abstract-class-in-ruby In the before he creates a new class and import the module to test, Then, using the class created, he test the module methods. Is this the recommended way to go? About testing validators I guess something like what was done with the models could also work but again not sure if its the way to go or if its totally wrong. Some advices on this matters will be really appreciated, Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ckponnappa at gmail.com Wed Aug 3 12:11:36 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Wed, 3 Aug 2011 21:41:36 +0530 Subject: [rspec-users] Rspec teting API. In-Reply-To: <6927012.4651.1312303849778.JavaMail.geo-discussion-forums@yqbp37> References: <6927012.4651.1312303849778.JavaMail.geo-discussion-forums@yqbp37> Message-ID: Your controller specs should suffice for API specs. Is there anything specific you want to do beyond assert the response code, content type and some basic assertions against the body of the response? You may want to take a look at http://github.com/c42/rspec-http for asserting against response codes and some headers. Best, Sidu. http://c42.in On 2 August 2011 22:20, Kleber Correia wrote: > Hi! > > Guys, I wanna test an API built in Rails 3.0.3. > > So, my question is... What's the better way to test my own api? > > I guess that a requests specs like these: > > describe 'POST /client' > it 'works' do > assert_difference post '/clients', :client => {...} > end > end > > can be appropriate... > > Someone just tested an API? I mean... simulate the requests to API. > Tks! > > _______________________________________________ > 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 chrismear at gmail.com Wed Aug 3 13:07:34 2011 From: chrismear at gmail.com (Chris Mear) Date: Wed, 3 Aug 2011 18:07:34 +0100 Subject: [rspec-users] Rspec with ActionMailer and .deliver In-Reply-To: References: Message-ID: On 27 July 2011 18:09, Todd Sedano wrote: > I have a controller that sends out an email through a mailer. > Rails 2 > code:?CurriculumCommentMailer.deliver_comment_update(@curriculum_comment, > "created") > Rails 3 code:?CurriculumCommentMailer.comment_update(@curriculum_comment, > "created").deliver > ?In my controller spec, I test to see if the email was sent out. > Rspec 1 > ?? ? ? ?it "emails the comment" do > ?? ? ? ? ?CurriculumCommentMailer.should_receive(:deliver_comment_update) > ?? ? ? ? ?post :create, :curriculum_comment => > @curriculum_comment.attributes > ?? ? ? ?end > Rspec 2 > In my opinion, I expected the following code to work > ?? ? ? it "emails the comment" do > ?? ? ? ? ?CurriculumCommentMailer.should_receive(:comment_update) > ?? ? ? ? ?post :create, :curriculum_comment => > @curriculum_comment.attributes > ?? ? ? ?end > However it does not. Since I'm calling should_receive on an object that > isn't a stub/mock/double, I expected should_receive to call the underlying > code, it does not so .deliver is called on a NilClass.?(undefined method > `deliver' for nil:NilClass) You're right -- #should_receive stubs out the object's underlying code, so it never gets called. > The following code does work > ?? ? ? ? ?mailer = double("mailer") > ?? ? ? ? ?mailer.stub(:deliver) > > ?CurriculumCommentMailer.should_receive(:comment_update).and_return(mailer) > Whereas I expected this code to work, but it does not either > > ?CurriculumCommentMailer.should_receive(:comment_update).and_return(double("mailer").stub(:deliver)) That's odd -- I would expect that second version to work if the first version is working. What error did you get? Was it the same "undefined method `deliver' for nil:NilClass" as before? Chris From dchelimsky at gmail.com Thu Aug 4 22:01:15 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 4 Aug 2011 21:01:15 -0500 Subject: [rspec-users] How to test modules and custom validators In-Reply-To: References: Message-ID: <5D3DFBA5-CFFB-49D2-B436-A8E916066A2C@gmail.com> On Aug 3, 2011, at 11:08 AM, Piter Fcbk wrote: > I'm trying to have most of the important stuff tested and in order to do that I run in front of some stuff I'm not quite sure which is the best way to test them. > At this time I am having question around how to test modules and the custom validators (the typical email format validator for example). > > To test a module the best I saw up to now was finded here: http://stackoverflow.com/questions/512466/how-to-implement-an-abstract-class-in-ruby > In the before he creates a new class and import the module to test, Then, using the class created, he test the module methods. > Is this the recommended way to go? > > About testing validators I guess something like what was done with the models could also work but again not sure if its the way to go or if its totally wrong. > > Some advices on this matters will be really appreciated, > > Thanks in advance. I usually like to do something like: describe MyModule do let(:object) { Class.new { include MyModule } } it "does something" do object.method_from_my_module.should do_something end end HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From cdemyanovich at gmail.com Thu Aug 4 23:13:38 2011 From: cdemyanovich at gmail.com (Craig Demyanovich) Date: Thu, 4 Aug 2011 22:13:38 -0500 Subject: [rspec-users] How to test modules and custom validators In-Reply-To: References: Message-ID: I recently wrote this spec for a custom validator. I hope you find it useful. Feedback welcome. Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Fri Aug 5 22:21:05 2011 From: lists at ruby-forum.com (John Sayeau) Date: Sat, 06 Aug 2011 04:21:05 +0200 Subject: [rspec-users] functional testing with(through?) chrome Message-ID: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> Not sure if this can be done using RSpec and something else but I'm trying to test a website using Chrome specifically. The website seems to intermittently hang up Chrome but not Safari and it passes functional tests without a browser. I want to run a test that loops through loading the site with chrome and measure the load time or record the error I get on a load fail. Hope this makes sense. -- Posted via http://www.ruby-forum.com/. From ckponnappa at gmail.com Sat Aug 6 01:57:42 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Sat, 6 Aug 2011 11:27:42 +0530 Subject: [rspec-users] functional testing with(through?) chrome In-Reply-To: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> References: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> Message-ID: I believe Cucumber + Sahi should work for you. More info here: http://blog.sahi.co.in/2010/04/sahi-vs-selenium.html Best, Sidu. http://c42.in On 6 August 2011 07:51, John Sayeau wrote: > Not sure if this can be done using RSpec and something else but I'm > trying to test a website using Chrome specifically. The website seems > to intermittently hang up Chrome but not Safari and it passes functional > tests without a browser. > I want to run a test that loops through loading the site with chrome and > measure the load time or record the error I get on a load fail. > > Hope this makes sense. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > 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 cjbottaro at gmail.com Fri Aug 5 13:55:55 2011 From: cjbottaro at gmail.com (Christopher J. Bottaro) Date: Fri, 5 Aug 2011 10:55:55 -0700 (PDT) Subject: [rspec-users] named context or calling include_context with block? Message-ID: Hello, I'm playing around with Rspec again after going from test/unit to rspec then back to test/unit... :) Right off the bat, I find myself wanting to do something like this: https://gist.github.com/1128091 Basically, I want to name a context, then call it later by name, passing a block to it. Or in other words... what is the best way to DRY this code? https://gist.github.com/1128108 Is that possible in Rspec 2.6? If not, can anyone provide a monkey patch for me? Thanks! From dchelimsky at gmail.com Sat Aug 6 08:56:20 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 6 Aug 2011 08:56:20 -0400 Subject: [rspec-users] named context or calling include_context with block? In-Reply-To: References: Message-ID: <8786ABF4-D107-4242-BFF4-C38FD31E3642@gmail.com> On Aug 5, 2011, at 1:55 PM, Christopher J. Bottaro wrote: > Hello, > > I'm playing around with Rspec again after going from test/unit to > rspec then back to test/unit... :) Right off the bat, I find myself > wanting to do something like this: https://gist.github.com/1128091 > > Basically, I want to name a context, then call it later by name, > passing a block to it. > > Or in other words... what is the best way to DRY this code? > https://gist.github.com/1128108 > > Is that possible in Rspec 2.6? Yes! Take a look at http://relishapp.com/rspec/rspec-core/dir/example-groups/shared-examples for some different options. > If not, can anyone provide a monkey > patch for me? Thanks! From cjbottaro at gmail.com Sat Aug 6 10:05:14 2011 From: cjbottaro at gmail.com (Christopher J. Bottaro) Date: Sat, 6 Aug 2011 07:05:14 -0700 (PDT) Subject: [rspec-users] named context or calling include_context with block? In-Reply-To: <8786ABF4-D107-4242-BFF4-C38FD31E3642@gmail.com> References: <8786ABF4-D107-4242-BFF4-C38FD31E3642@gmail.com> Message-ID: <3b7b3e64-6771-4436-ad8e-93a85a4d2fcd@e7g2000vbw.googlegroups.com> Hey, Maybe I'm reading that wrong, but that's not exactly what I want to do. In my example, when I call the shared context, I'm passing it a block. Neither include_context or include_examples take a block when called. Thanks. On Aug 6, 8:56?am, David Chelimsky wrote: > On Aug 5, 2011, at 1:55 PM, Christopher J. Bottaro wrote: > > > Hello, > > > I'm playing around with Rspec again after going from test/unit to > > rspec then back to test/unit... :) ?Right off the bat, I find myself > > wanting to do something like this: ?https://gist.github.com/1128091 > > > Basically, I want to name a context, then call it later by name, > > passing a block to it. > > > Or in other words... what is the best way to DRY this code? > >https://gist.github.com/1128108 > > > Is that possible in Rspec 2.6? > > Yes! Take a look athttp://relishapp.com/rspec/rspec-core/dir/example-groups/shared-examplesfor some different options. > > > If not, can anyone provide a monkey > > patch for me? ?Thanks! > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Sat Aug 6 11:57:25 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 6 Aug 2011 11:57:25 -0400 Subject: [rspec-users] named context or calling include_context with block? In-Reply-To: <3b7b3e64-6771-4436-ad8e-93a85a4d2fcd@e7g2000vbw.googlegroups.com> References: <8786ABF4-D107-4242-BFF4-C38FD31E3642@gmail.com> <3b7b3e64-6771-4436-ad8e-93a85a4d2fcd@e7g2000vbw.googlegroups.com> Message-ID: <3D40B10B-8A7B-4572-A61B-B0752C2CB3C5@gmail.com> On Aug 6, 2011, at 10:05 AM, Christopher J. Bottaro wrote: > On Aug 6, 8:56 am, David Chelimsky wrote: >> On Aug 5, 2011, at 1:55 PM, Christopher J. Bottaro wrote: >> >>> Hello, >> >>> I'm playing around with Rspec again after going from test/unit to >>> rspec then back to test/unit... :) Right off the bat, I find myself >>> wanting to do something like this: https://gist.github.com/1128091 >> >>> Basically, I want to name a context, then call it later by name, >>> passing a block to it. >> >>> Or in other words... what is the best way to DRY this code? >>> https://gist.github.com/1128108 >> >>> Is that possible in Rspec 2.6? >> >> Yes! Take a look athttp://relishapp.com/rspec/rspec-core/dir/example-groups/shared-examplesfor some different options. > Hey, > > Maybe I'm reading that wrong, but that's not exactly what I want to > do. In my example, when I call the shared context, I'm passing it a > block. Neither include_context or include_examples take a block when > called. > > Thanks. it_behaves_like does, and it can be aliased, so you could so something like https://gist.github.com/1129417. HTH, David ps - I moved your reply inline. Please post inline or at the bottom, especially if the conversation is already structured that way. See http://idallen.com/topposting.html for rationale. >>> If not, can anyone provide a monkey >>> patch for me? Thanks! From lists at ruby-forum.com Sat Aug 6 18:08:52 2011 From: lists at ruby-forum.com (John Sayeau) Date: Sun, 07 Aug 2011 00:08:52 +0200 Subject: [rspec-users] functional testing with(through?) chrome In-Reply-To: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> References: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> Message-ID: Thank you. I'll check it out. John -- Posted via http://www.ruby-forum.com/. From matt at mattwynne.net Sun Aug 7 07:01:27 2011 From: matt at mattwynne.net (Matt Wynne) Date: Sun, 7 Aug 2011 12:01:27 +0100 Subject: [rspec-users] functional testing with(through?) chrome In-Reply-To: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> References: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> Message-ID: <90044114-C74D-4F6C-906C-DB47736A140A@mattwynne.net> On 6 Aug 2011, at 03:21, John Sayeau wrote: > Not sure if this can be done using RSpec and something else but I'm > trying to test a website using Chrome specifically. The website seems > to intermittently hang up Chrome but not Safari and it passes functional > tests without a browser. > I want to run a test that loops through loading the site with chrome and > measure the load time or record the error I get on a load fail. > > Hope this makes sense. I would recommend Capybara[1] with the Selenium driver configured to use Chrome: Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, :browser => :chrome) end [1] https://github.com/jnicklas/capybara cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Helles?y) Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From cjbottaro at gmail.com Sun Aug 7 13:17:45 2011 From: cjbottaro at gmail.com (Christopher J. Bottaro) Date: Sun, 7 Aug 2011 10:17:45 -0700 (PDT) Subject: [rspec-users] named context or calling include_context with block? In-Reply-To: <3D40B10B-8A7B-4572-A61B-B0752C2CB3C5@gmail.com> References: <8786ABF4-D107-4242-BFF4-C38FD31E3642@gmail.com> <3b7b3e64-6771-4436-ad8e-93a85a4d2fcd@e7g2000vbw.googlegroups.com> <3D40B10B-8A7B-4572-A61B-B0752C2CB3C5@gmail.com> Message-ID: <214cfce7-da95-4aa8-9ef2-fb02a25f788e@z14g2000yqh.googlegroups.com> On Aug 6, 11:57?am, David Chelimsky wrote: > On Aug 6, 2011, at 10:05 AM, Christopher J. Bottaro wrote: > > > > > > > > > > > On Aug 6, 8:56 am, David Chelimsky wrote: > >> On Aug 5, 2011, at 1:55 PM, Christopher J. Bottaro wrote: > > >>> Hello, > > >>> I'm playing around with Rspec again after going from test/unit to > >>> rspec then back to test/unit... :) ?Right off the bat, I find myself > >>> wanting to do something like this: ?https://gist.github.com/1128091 > > >>> Basically, I want to name a context, then call it later by name, > >>> passing a block to it. > > >>> Or in other words... what is the best way to DRY this code? > >>>https://gist.github.com/1128108 > > >>> Is that possible in Rspec 2.6? > > >> Yes! Take a look athttp://relishapp.com/rspec/rspec-core/dir/example-groups/shared-examp...some different options. > > Hey, > > > Maybe I'm reading that wrong, but that's not exactly what I want to > > do. ?In my example, when I call the shared context, I'm passing it a > > block. ?Neither include_context or include_examples take a block when > > called. > > > Thanks. > > it_behaves_like does, and it can be aliased, so you could so something like https://gist.github.com/1129417 . Awesome, that's exactly what I'm looking for! I guess I was confused with the examples on relishapp because I thought it would just print out "it behaves like Module" instead of printing out each individual example. One quick thing though... I couldn't use the constant MyModule with shared_examples and alias to it_should_behave_like. I had to change it to a string "MyModule" then everything worked. I'm using RSpec 2.6.4. Thank you, -- C > HTH, > David > > ps - I moved your reply inline. Please post inline or at the bottom, especially if the conversation is already structured that way. Seehttp://idallen.com/topposting.htmlfor rationale. > > >>> If not, can anyone provide a monkey > >>> patch for me? ?Thanks! > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Sun Aug 7 17:16:12 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 7 Aug 2011 16:16:12 -0500 Subject: [rspec-users] named context or calling include_context with block? In-Reply-To: <214cfce7-da95-4aa8-9ef2-fb02a25f788e@z14g2000yqh.googlegroups.com> References: <8786ABF4-D107-4242-BFF4-C38FD31E3642@gmail.com> <3b7b3e64-6771-4436-ad8e-93a85a4d2fcd@e7g2000vbw.googlegroups.com> <3D40B10B-8A7B-4572-A61B-B0752C2CB3C5@gmail.com> <214cfce7-da95-4aa8-9ef2-fb02a25f788e@z14g2000yqh.googlegroups.com> Message-ID: <546DEBEE-4223-40F6-A22B-67048E4CE570@gmail.com> On Aug 7, 2011, at 12:17 PM, Christopher J. Bottaro wrote: > One quick thing though... I couldn't use the constant MyModule with > shared_examples and alias to it_should_behave_like. I had to change > it to a string "MyModule" then everything worked. I'm using RSpec > 2.6.4. That's fixed in git, will be released in rspec 2.7.0. Cheers, David From matt at mattwynne.net Mon Aug 8 19:00:51 2011 From: matt at mattwynne.net (Matt Wynne) Date: Tue, 9 Aug 2011 00:00:51 +0100 Subject: [rspec-users] Speccing a redirect in routes Message-ID: Hi, I expected to be able to do something like this in a routing spec: { :get => '/legacy/route' }.should redirect_to('/shiny/new/route') However there doesn't appear to be a redirect_to matcher, and digging deeper I can't even see an underlying Rails assertion method that will assert for redirect routing configuration. Did I miss it? Do I need to spec this with a request spec instead? cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Helles?y) Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne From dchelimsky at gmail.com Mon Aug 8 20:02:04 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 8 Aug 2011 19:02:04 -0500 Subject: [rspec-users] Speccing a redirect in routes In-Reply-To: References: Message-ID: On Aug 8, 2011, at 6:00 PM, Matt Wynne wrote: > Hi, > > I expected to be able to do something like this in a routing spec: > > { :get => '/legacy/route' }.should redirect_to('/shiny/new/route') Routes resolve to paths to controller actions, they don't do any http redirection. > However there doesn't appear to be a redirect_to matcher, and digging deeper I can't even see an underlying Rails assertion method that will assert for redirect routing configuration. Did I miss it? > > Do I need to spec this with a request spec instead? Or a controller spec. Either way, it's the controller that's redirecting Cheers, David From chrismear at gmail.com Tue Aug 9 03:00:05 2011 From: chrismear at gmail.com (Chris Mear) Date: Tue, 9 Aug 2011 08:00:05 +0100 Subject: [rspec-users] Speccing a redirect in routes In-Reply-To: References: Message-ID: <4E0CEA71-CC2A-437E-B47F-E4481B4EB77D@gmail.com> On 9 Aug 2011, at 01:02, David Chelimsky wrote: > On Aug 8, 2011, at 6:00 PM, Matt Wynne wrote: > >> I expected to be able to do something like this in a routing spec: >> >> { :get => '/legacy/route' }.should redirect_to('/shiny/new/route') > > Routes resolve to paths to controller actions, they don't do any http redirection. A 'redirect' helper was added to ActionDispatch::Mapper for Rails 3, to allow redirection without hitting a controller: https://github.com/rails/rails/commit/a1ce52e#L2R294 http://guides.rubyonrails.org/routing.html#redirection Chris From dchelimsky at gmail.com Tue Aug 9 08:11:34 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 9 Aug 2011 07:11:34 -0500 Subject: [rspec-users] Speccing a redirect in routes In-Reply-To: <4E0CEA71-CC2A-437E-B47F-E4481B4EB77D@gmail.com> References: <4E0CEA71-CC2A-437E-B47F-E4481B4EB77D@gmail.com> Message-ID: On Aug 9, 2011, at 2:00 AM, Chris Mear wrote: > On 9 Aug 2011, at 01:02, David Chelimsky wrote: > >> On Aug 8, 2011, at 6:00 PM, Matt Wynne wrote: >> >>> I expected to be able to do something like this in a routing spec: >>> >>> { :get => '/legacy/route' }.should redirect_to('/shiny/new/route') >> >> Routes resolve to paths to controller actions, they don't do any http redirection. > > A 'redirect' helper was added to ActionDispatch::Mapper for Rails 3, to allow redirection without hitting a controller: > > https://github.com/rails/rails/commit/a1ce52e#L2R294 > > http://guides.rubyonrails.org/routing.html#redirection > > Chris Glad to know that! I think this needs to be in Rails before it's in RSpec. Would either of you (Matt/Chris) care to open up a feature request for a new Rails assertion? Something like assert_route_redirects? If you do, please reference it in this thread. Cheers, David From lenny at aps.org Wed Aug 10 11:13:15 2011 From: lenny at aps.org (Lenny Marks) Date: Wed, 10 Aug 2011 11:13:15 -0400 Subject: [rspec-users] ControllerExampleGroup.bypass_rescue Message-ID: <670AF299-FCAF-4053-BF1A-628C7053F9CD@aps.org> As best I can tell, bypass_rescue from rspec-rails-1 is no longer part of rspec-rails, '> 2'. I had been using it on occasion for things like: describe CorrespondencesController do ... describe '#show' do it "should raise an AuthorizationError if current user is not the correspondent " do bypass_rescue ... expect { do_get }.to raise_error(AuthorizationError) I know there are conflicting opinions on whether or not it's a good idea to directly check for exceptions this way, but I've always felt that this was appropriate for testing a controller action in isolation where the responsibility of the action under test was only to raise the error. Anyway, I couldn't find any recent references to this (not even in the rspec-rails repo). Is there any way to do this in rspec-rails-2 or is the official consensus to check only on response codes, etc... ? -lenny From matt at mattwynne.net Wed Aug 10 18:09:35 2011 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 10 Aug 2011 23:09:35 +0100 Subject: [rspec-users] Speccing a redirect in routes In-Reply-To: References: <4E0CEA71-CC2A-437E-B47F-E4481B4EB77D@gmail.com> Message-ID: <1D48EC99-D261-41A6-B4B4-7A732D46B68A@mattwynne.net> On 9 Aug 2011, at 13:11, David Chelimsky wrote: > On Aug 9, 2011, at 2:00 AM, Chris Mear wrote: > >> On 9 Aug 2011, at 01:02, David Chelimsky wrote: >> >>> On Aug 8, 2011, at 6:00 PM, Matt Wynne wrote: >>> >>>> I expected to be able to do something like this in a routing spec: >>>> >>>> { :get => '/legacy/route' }.should redirect_to('/shiny/new/route') >>> >>> Routes resolve to paths to controller actions, they don't do any http redirection. >> >> A 'redirect' helper was added to ActionDispatch::Mapper for Rails 3, to allow redirection without hitting a controller: >> >> https://github.com/rails/rails/commit/a1ce52e#L2R294 >> >> http://guides.rubyonrails.org/routing.html#redirection >> >> Chris > > > Glad to know that! > > I think this needs to be in Rails before it's in RSpec. Would either of you (Matt/Chris) care to open up a feature request for a new Rails assertion? Something like assert_route_redirects? If you do, please reference it in this thread. Done: https://github.com/rails/rails/issues/2488 cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Helles?y) Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Thu Aug 11 08:17:38 2011 From: jko170 at gmail.com (Justin Ko) Date: Thu, 11 Aug 2011 08:17:38 -0400 Subject: [rspec-users] ControllerExampleGroup.bypass_rescue In-Reply-To: <670AF299-FCAF-4053-BF1A-628C7053F9CD@aps.org> References: <670AF299-FCAF-4053-BF1A-628C7053F9CD@aps.org> Message-ID: <519BE192-A45E-4498-8A90-47D53B45A012@gmail.com> If you are rescuing an exception, test what the rescue does. Purposely cause the exception, then check the rescue does what it's supposed to. Sent from my iPhone On Aug 10, 2011, at 11:13 AM, Lenny Marks wrote: > As best I can tell, bypass_rescue from rspec-rails-1 is no longer part of rspec-rails, '> 2'. I had been using it on occasion for things like: > > describe CorrespondencesController do > ... > describe '#show' do > it "should raise an AuthorizationError if current user is not the correspondent " do > bypass_rescue > ... > expect { do_get }.to raise_error(AuthorizationError) > > I know there are conflicting opinions on whether or not it's a good idea to directly check for exceptions this way, but I've always felt that this was appropriate for testing a controller action in isolation where the responsibility of the action under test was only to raise the error. > > Anyway, I couldn't find any recent references to this (not even in the rspec-rails repo). Is there any way to do this in rspec-rails-2 or is the official consensus to check only on response codes, etc... ? > > -lenny > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Thu Aug 11 08:40:33 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 11 Aug 2011 07:40:33 -0500 Subject: [rspec-users] ControllerExampleGroup.bypass_rescue In-Reply-To: <519BE192-A45E-4498-8A90-47D53B45A012@gmail.com> References: <670AF299-FCAF-4053-BF1A-628C7053F9CD@aps.org> <519BE192-A45E-4498-8A90-47D53B45A012@gmail.com> Message-ID: On Aug 11, 2011, at 7:17 AM, Justin Ko wrote: > On Aug 10, 2011, at 11:13 AM, Lenny Marks wrote: > >> As best I can tell, bypass_rescue from rspec-rails-1 is no longer part of rspec-rails, '> 2'. I had been using it on occasion for things like: >> >> describe CorrespondencesController do >> ... >> describe '#show' do >> it "should raise an AuthorizationError if current user is not the correspondent " do >> bypass_rescue >> ... >> expect { do_get }.to raise_error(AuthorizationError) >> >> I know there are conflicting opinions on whether or not it's a good idea to directly check for exceptions this way, but I've always felt that this was appropriate for testing a controller action in isolation where the responsibility of the action under test was only to raise the error. >> >> Anyway, I couldn't find any recent references to this (not even in the rspec-rails repo). Is there any way to do this in rspec-rails-2 or is the official consensus to check only on response codes, etc... ? >> >> -lenny > > If you are rescuing an exception, test what the rescue does. Purposely cause the exception, then check the rescue does what it's supposed to. Justin - that specifies how the controller handles exceptions, but not when they get raised, which is the purpose of bypass_rescue. In Lenny's example, above, the GET results in an AuthorizationError, but if that gets rescued then you have to specify it in terms of what the rescue does, not how it got to the rescue handler. That's probably OK in most cases, but there are cases where you have more than one way to get to a rescue, and you want to specify the details of how it gets there. Lenny, this was an oversight. Please submit an issue to http://github.com/rspec/rspec-rails/issues and we'll see about getting this reinstated. No guarantees since so much changed in Rails between v2 and v3, but if it's reasonable to add I think we should. Cheers, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Thu Aug 11 08:59:05 2011 From: jko170 at gmail.com (Justin Ko) Date: Thu, 11 Aug 2011 08:59:05 -0400 Subject: [rspec-users] ControllerExampleGroup.bypass_rescue In-Reply-To: References: <670AF299-FCAF-4053-BF1A-628C7053F9CD@aps.org> <519BE192-A45E-4498-8A90-47D53B45A012@gmail.com> Message-ID: On Thu, Aug 11, 2011 at 8:40 AM, David Chelimsky wrote: > On Aug 11, 2011, at 7:17 AM, Justin Ko wrote: > > On Aug 10, 2011, at 11:13 AM, Lenny Marks wrote: > > As best I can tell, bypass_rescue from rspec-rails-1 is no longer part of > rspec-rails, '> 2'. I had been using it on occasion for things like: > > > describe CorrespondencesController do > > ... > > describe '#show' do > > it "should raise an AuthorizationError if current user is not the > correspondent " do > > bypass_rescue > > ... > > expect { do_get }.to raise_error(AuthorizationError) > > > I know there are conflicting opinions on whether or not it's a good idea to > directly check for exceptions this way, but I've always felt that this was > appropriate for testing a controller action in isolation where the > responsibility of the action under test was only to raise the error. > > > Anyway, I couldn't find any recent references to this (not even in the > rspec-rails repo). Is there any way to do this in rspec-rails-2 or is the > official consensus to check only on response codes, etc... ? > > > -lenny > > > If you are rescuing an exception, test what the rescue does. Purposely > cause the exception, then check the rescue does what it's supposed to. > > > Justin - that specifies how the controller handles exceptions, but not when > they get raised, which is the purpose of bypass_rescue. In Lenny's example, > above, the GET results in an AuthorizationError, but if that gets rescued > then you have to specify it in terms of what the rescue does, not how it got > to the rescue handler. That's probably OK in most cases, but there are cases > where you have more than one way to get to a rescue, and you want to specify > the details of how it gets there. > > Lenny, this was an oversight. Please submit an issue to > http://github.com/rspec/rspec-rails/issues and we'll see about getting > this reinstated. No guarantees since so much changed in Rails between v2 and > v3, but if it's reasonable to add I think we should. > > Cheers, > David > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > "there are cases where you have more than one way to get to a rescue" Wouldn't you just mock/do-whatever to ensure the correct path to the rescue for the particular example? -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan.kanev at gmail.com Thu Aug 11 09:27:48 2011 From: stefan.kanev at gmail.com (Stefan Kanev) Date: Thu, 11 Aug 2011 16:27:48 +0300 Subject: [rspec-users] ControllerExampleGroup.bypass_rescue In-Reply-To: References: <670AF299-FCAF-4053-BF1A-628C7053F9CD@aps.org> <519BE192-A45E-4498-8A90-47D53B45A012@gmail.com> Message-ID: I agree with Lenny. I can give an example. Lets say that parts of the application are restricted. Whenever they are accessed by an unauthorized user, they trigger UnauthorizedAccessError. Depending on the role the user has in the system, different actions should be performed, e.g. unauthenticated users get redirected to the hope page, admins see a special message with more details. The example is a bit synthetic, but I'm sure you can see a similar, real one. In the design I have in mind, the logic that handles the UnauthorizedAccessError is handled in ApplicationController, while the error is raised in some specific controller. If you, in this error-raising path, assert that a non-authenticated user gets redirected to the homepage, you're encoding that behavior in the test. But this behavior is only accidental to the controller under test. This makes the test harder to understand. Worse, if you have multiple actions that raise this exception, you're encoding the knowledge in multiple tests. Now you have duplication too. I think it makes much more sense to assert that UnauthorizedAccessError is raised and specify what happens elsewhere. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alex at stinky.com Fri Aug 12 19:03:14 2011 From: alex at stinky.com (Alex Chaffee) Date: Fri, 12 Aug 2011 16:03:14 -0700 Subject: [rspec-users] improving TextMate RSpec Bundle's error output Message-ID: I just posted a pull request to improve the RSpec TextMate Bundle's output in cases where the error occurs *outside* a spec -- e.g. a syntax error. It's currently quite ugly and my patch formats it as HTML and adds some contextual info. patch: https://github.com/rspec/rspec-tmbundle/pull/30 screenshots: https://docs.google.com/document/pub?id=1qSWcCl2M931MCfYW2YGdXSJipnG6ZiLhMu74ubKvMl4 The reason I'm posting is not to plead for +1 love for the patch (though that would be nice) but to ask about a feature I stuck in there as well, that's potentially controversial. It grabs standard error output and shows it (formatted as a PRE block) at the bottom of the results in the output window. I say it's potentially controversial because some people might want to use $stderr.puts as debug info in the middle of a run; those lines currently show up just above the relevant test name, but in this PRE patch, that proximity is lost. So let me please take a quick poll. Who wants A. The current behavior, where $stderr.puts (and warn, and regular puts/p) output shows up interlaced in the textmate output window, unformatted and unescaped B. The behavior in my 2nd patch, where $stderr is captured and then displayed at the end of the run, below the nice colored HTML test results, in a PRE block so it's easier to read C. New unimplemented behavior, where $stderr is replaced with a smart object that wraps lines in PRE, but still interlaces them with the more-pretty test results as they happen Non-TextMate users need not reply :-) - A -- Alex Chaffee - alex at stinky.com - http://alexch.github.com Stalk me: http://friendfeed.com/alexch | http://twitter.com/alexch | http://alexch.tumblr.com From john.hinnegan at gmail.com Sun Aug 14 17:48:14 2011 From: john.hinnegan at gmail.com (John Hinnegan) Date: Sun, 14 Aug 2011 14:48:14 -0700 Subject: [rspec-users] stub in config.before Message-ID: I was trying to stub something "globally", across all tests today, and had some trouble. I tried RSpec.configure do |config| config.before(:all) do MyClass.stub(:my_method) end end seems that stub is not available here yet. (I was stubbing a class method, and the class was loaded correctly). Is there somewhere else I should be putting this? At what point is the stub code loaded? (PS, the code I'm using in there works just fine in "proper" before blocks inside describe blocks) -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Sun Aug 14 23:15:35 2011 From: jko170 at gmail.com (Justin Ko) Date: Sun, 14 Aug 2011 21:15:35 -0600 Subject: [rspec-users] stub in config.before In-Reply-To: References: Message-ID: On Sun, Aug 14, 2011 at 3:48 PM, John Hinnegan wrote: > > > I was trying to stub something "globally", across all tests today, and had > some trouble. > > I tried > > RSpec.configure do |config| > config.before(:all) do > MyClass.stub(:my_method) > end > end > > seems that stub is not available here yet. (I was stubbing a class method, > and the class was loaded correctly). > > Is there somewhere else I should be putting this? At what point is the > stub code loaded? > > (PS, the code I'm using in there works just fine in "proper" before blocks > inside describe blocks) > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users That *should* work. The before's in config are run before any before's in your specs. And as long as you're not using any other mocking framework, `.stub` should work as well. We'll need some more info to help you debug. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Sun Aug 14 23:17:24 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 14 Aug 2011 22:17:24 -0500 Subject: [rspec-users] stub in config.before In-Reply-To: References: Message-ID: On Aug 14, 2011, at 10:15 PM, Justin Ko wrote: > On Sun, Aug 14, 2011 at 3:48 PM, John Hinnegan wrote: > > > I was trying to stub something "globally", across all tests today, and had some trouble. > > I tried > > RSpec.configure do |config| > config.before(:all) do > MyClass.stub(:my_method) > end > end > > seems that stub is not available here yet. (I was stubbing a class method, and the class was loaded correctly). > > Is there somewhere else I should be putting this? At what point is the stub code loaded? > > (PS, the code I'm using in there works just fine in "proper" before blocks inside describe blocks) > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > That *should* work. The before's in config are run before any before's in your specs. And as long as you're not using any other mocking framework, `.stub` should work as well. We'll need some more info to help you debug. The problem is not that it's a global config, but it's in before(:all). Put it in a before(:each) and it will work. Cheers, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From john.hinnegan at gmail.com Sun Aug 14 23:31:41 2011 From: john.hinnegan at gmail.com (John Hinnegan) Date: Sun, 14 Aug 2011 20:31:41 -0700 Subject: [rspec-users] stub in config.before In-Reply-To: References: Message-ID: the error I got was that .stub was not defined on the class from that block I can try to repro it outside of my current project later if you need actual failing code, but that's a representative snippet of what I tried On Sun, Aug 14, 2011 at 8:15 PM, Justin Ko wrote: > > > On Sun, Aug 14, 2011 at 3:48 PM, John Hinnegan wrote: > >> >> >> I was trying to stub something "globally", across all tests today, and had >> some trouble. >> >> I tried >> >> RSpec.configure do |config| >> config.before(:all) do >> MyClass.stub(:my_method) >> end >> end >> >> seems that stub is not available here yet. (I was stubbing a class >> method, and the class was loaded correctly). >> >> Is there somewhere else I should be putting this? At what point is the >> stub code loaded? >> >> (PS, the code I'm using in there works just fine in "proper" before blocks >> inside describe blocks) >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > That *should* work. The before's in config are run before any before's in > your specs. And as long as you're not using any other mocking framework, > `.stub` should work as well. We'll need some more info to help you debug. > > > _______________________________________________ > 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 john.hinnegan at gmail.com Sun Aug 14 23:41:10 2011 From: john.hinnegan at gmail.com (John Hinnegan) Date: Sun, 14 Aug 2011 20:41:10 -0700 Subject: [rspec-users] stub in config.before In-Reply-To: References: Message-ID: Thanks, cheers On Sun, Aug 14, 2011 at 8:17 PM, David Chelimsky wrote: > On Aug 14, 2011, at 10:15 PM, Justin Ko wrote: > > On Sun, Aug 14, 2011 at 3:48 PM, John Hinnegan wrote: > >> >> >> I was trying to stub something "globally", across all tests today, and had >> some trouble. >> >> I tried >> >> RSpec.configure do |config| >> config.before(:all) do >> MyClass.stub(:my_method) >> end >> end >> >> seems that stub is not available here yet. (I was stubbing a class >> method, and the class was loaded correctly). >> >> Is there somewhere else I should be putting this? At what point is the >> stub code loaded? >> >> (PS, the code I'm using in there works just fine in "proper" before blocks >> inside describe blocks) >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > That *should* work. The before's in config are run before any before's in > your specs. And as long as you're not using any other mocking framework, > `.stub` should work as well. We'll need some more info to help you debug. > > > The problem is not that it's a global config, but it's in before(:all). Put > it in a before(:each) and it will work. > > Cheers, > David > > _______________________________________________ > 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 Aug 15 08:56:33 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 15 Aug 2011 07:56:33 -0500 Subject: [rspec-users] stub in config.before In-Reply-To: References: Message-ID: <242B51AE-E770-4CF3-AC5A-50CFFAC317AB@gmail.com> On Aug 14, 2011, at 10:31 PM, John Hinnegan wrote: > > On Sun, Aug 14, 2011 at 8:15 PM, Justin Ko wrote: > > > On Sun, Aug 14, 2011 at 3:48 PM, John Hinnegan wrote: > > > I was trying to stub something "globally", across all tests today, and had some trouble. > > I tried > > RSpec.configure do |config| > config.before(:all) do > MyClass.stub(:my_method) > end > end > > seems that stub is not available here yet. (I was stubbing a class method, and the class was loaded correctly). > > Is there somewhere else I should be putting this? At what point is the stub code loaded? > > (PS, the code I'm using in there works just fine in "proper" before blocks inside describe blocks) > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > That *should* work. The before's in config are run before any before's in your specs. And as long as you're not using any other mocking framework, `.stub` should work as well. We'll need some more info to help you debug. > the error I got was that .stub was not defined on the class from that block > > I can try to repro it outside of my current project later if you need actual failing code, but that's a representative snippet of what I tried Did you try using before(:each)? David -------------- next part -------------- An HTML attachment was scrubbed... URL: From tute.unique at gmail.com Sat Aug 6 12:50:38 2011 From: tute.unique at gmail.com (Matias) Date: Sat, 6 Aug 2011 09:50:38 -0700 (PDT) Subject: [rspec-users] Specs doesn't run Message-ID: Rails 2.3.8 GEMFILE group :development do gem "rspec", "1.3.0" gem "rspec-rails", "1.3.2" gem "guard-rspec" #https://github.com/guard/guard-rspec end SPEC_HELPER.RB # This file is copied to ~/spec when you run 'ruby script/generate rspec' # from the project root directory. ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment')) require 'spec/autorun' require 'spec/rails' # Uncomment the next line to use webrat's matchers #require 'webrat/integrations/rspec-rails' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f} Spec::Runner.configure do |config| # If you're not using ActiveRecord you should remove these # lines, delete config/database.yml and disable :active_record # in your config/boot.rb config.use_transactional_fixtures = true config.use_instantiated_fixtures = false config.fixture_path = RAILS_ROOT + '/spec/fixtures/' # == Fixtures # # You can declare fixtures for each example_group like this: # describe "...." do # fixtures :table_a, :table_b # # Alternatively, if you prefer to declare them only once, you can # do so right here. Just uncomment the next line and replace the fixture # names with your fixtures. # # config.global_fixtures = :table_a, :table_b # # If you declare global fixtures, be aware that they will be declared # for all of your examples, even those that don't use them. # # You can also declare which fixtures to use (for example fixtures for test/fixtures): # # config.fixture_path = RAILS_ROOT + '/spec/fixtures/' # # == Mock Framework # # RSpec uses its own mocking framework by default. If you prefer to # use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr # # == Notes # # For more information take a look at Spec::Runner::Configuration and Spec::Runner end And my CAMPAIGN_SPEC.rb require 'spec_helper' require 'campaign' describe "Campaign" do describe "in first step" do before(:each) do @user = User.new @user.save(false) @campaign = Campaign.new(:user => @user) end it "should have an owner" do @campaign.owner.should == @user end end end I run the specs with guard, spec spec/models/campaign_spec.rb or rake spec:all but I don't get any output. From nick at deadorange.com Mon Aug 8 11:36:06 2011 From: nick at deadorange.com (Nick) Date: Mon, 8 Aug 2011 08:36:06 -0700 (PDT) Subject: [rspec-users] Stubbing #render_to_string overrides #render Message-ID: <8514586.484.1312817766637.JavaMail.geo-discussion-forums@vbhc7> Hey folks. In my controller specs, when I stub #render_to_string, #render is also stubbed out. Thus, if a controller action calls #render, the stubbed #render returns what #render_to_string returns, which causes specs to fail. http://pastie.org/2339809 Am I doing something strange or wrong? If not, how can I stub #render_to_string without affecting #render ? Thanks, Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: From tute.unique at gmail.com Mon Aug 8 20:37:01 2011 From: tute.unique at gmail.com (Matias) Date: Mon, 8 Aug 2011 17:37:01 -0700 (PDT) Subject: [rspec-users] Webrat and Rails 2.3.12 Message-ID: <3e373512-464f-4173-ac7e-e65d31b85fac@a4g2000yqg.googlegroups.com> Im trying to integrate webrat in a rails 2.3.12: I get this OUTPUT: 1) NoMethodError in 'Campaign in first step testing request' undefined method `visit' for # /home/matias/livechains/branches/test/livechains/spec/models/ campaign_spec.rb:16: Finished in 6.518574 seconds 2 examples, 1 failure My app is like this: Gemfile group :development, :test do #gem 'fiveruns_tuneup' #autotest-rails, test_notifier, ZenTest, (sudo apt-get installlinux libnotify-bin), autotest-notification, gem "rspec", "1.3.0" gem "rspec-rails", "1.3.2" #necesita lib-notify e inotify en linux gem "guard-rspec" #https://github.com/guard/guard-rspec gem 'ffi' gem 'libnotify' gem 'factory_girl' gem 'webrat' gem 'test-unit', '1.2.3' gem 'hoe', '2.8.0' end spec_helper.rb ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment')) require 'spec/autorun' require 'spec/rails' require 'webrat' # Uncomment the next line to use webrat's matchers #require 'webrat/integrations/rspec-rails' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f} Spec::Runner.configure do |config| # If you're not using ActiveRecord you should remove these # lines, delete config/database.yml and disable :active_record # in your config/boot.rb config.use_transactional_fixtures = true config.use_instantiated_fixtures = false config.fixture_path = RAILS_ROOT + '/spec/fixtures/' # include Webrat::Methods #config.include Webrat::Matchers, :type => :views end Webrat.configure do |config| config.mode = :rails config.open_error_files = false # prevents webrat from opening the browser end spec/models/campaign_spec.rb require 'spec_helper' describe "Campaign" do describe "in first step" do before(:each) do @user = Factory(:user) @campaign = Factory(:campaign, :owner => @user) end it "should have an owner" do @campaign.owner.should == @user end it "testing request" do visit "/" end end end From tute.unique at gmail.com Tue Aug 9 08:20:23 2011 From: tute.unique at gmail.com (Matias) Date: Tue, 9 Aug 2011 05:20:23 -0700 (PDT) Subject: [rspec-users] Devise, RSpec and Webrat Message-ID: <5e04b78e-9628-4c0b-83a5-838261fa720f@e35g2000yqc.googlegroups.com> My app is builded with rails 2.3.12, rspec-rails 1.3.2, webrat 0.7.3. My spec_helper.rb ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment')) require 'spec/autorun' require 'spec/rails' require 'webrat' Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f} Webrat.configure do |config| config.mode = :rails config.open_error_files = false # prevents webrat from opening the browser end # Uncomment the next line to use webrat's matchers require 'webrat/integrations/rspec-rails' Spec::Runner.configure do |config| config.use_transactional_fixtures = true config.use_instantiated_fixtures = false config.fixture_path = RAILS_ROOT + '/spec/fixtures/' include Webrat::Methods #config.include Webrat::Matchers, :type => :views end My spec: require 'spec_helper' describe "Campaign" do describe "in first step" do before(:each) do @user = Factory(:user) @campaign = Factory(:campaign, :owner => @user) end it "should have an owner" do @campaign.owner.should == @user end it "testing request" do visit "/" end end end I get: ActionView::TemplateError in 'Campaign in first step should have an owner' super: no superclass method `response' for # In app/views/devise_mailer/confirmation_instructions.html.erb webrat (0.7.3) lib/webrat/core/methods.rb:31:in `response' /usr/lib/ruby/gems/1.8/gems/rspec-rails-1.3.2/lib/spec/rails/ extensions/action_view/base.rb:27:in `render' devise (1.0.9) app/models/devise_mailer.rb:42:in `render_with_scope' devise (1.0.9) app/models/devise_mailer.rb:31:in `setup_mail' devise (1.0.9) app/models/devise_mailer.rb:7:in `confirmation_instructions' devise (1.0.9) lib/devise/models/confirmable.rb:61:in `send_confirmation_instructions' /usr/lib/ruby/gems/1.8/gems/factory_girl-2.0.3/lib/factory_girl/ proxy/create.rb:9:in `result' /usr/lib/ruby/gems/1.8/gems/factory_girl-2.0.3/lib/factory_girl/ factory.rb:86:in `run' /usr/lib/ruby/gems/1.8/gems/factory_girl-2.0.3/lib/factory_girl/ syntax/methods.rb:54:in `create' /usr/lib/ruby/gems/1.8/gems/factory_girl-2.0.3/lib/factory_girl/ syntax/vintage.rb:53:in `send' /usr/lib/ruby/gems/1.8/gems/factory_girl-2.0.3/lib/factory_girl/ syntax/vintage.rb:53:in `default_strategy' /usr/lib/ruby/gems/1.8/gems/factory_girl-2.0.3/lib/factory_girl/ syntax/vintage.rb:146:in `Factory' spec/models/campaign_spec.rb:7 rspec (1.3.0) lib/spec/example/example_methods.rb:74:in `instance_eval' rspec (1.3.0) lib/spec/example/example_methods.rb:74:in `eval_each_fail_fast' rspec (1.3.0) lib/spec/example/example_methods.rb:74:in `each' rspec (1.3.0) lib/spec/example/example_methods.rb:74:in `eval_each_fail_fast' rspec (1.3.0) lib/spec/example/example_group_hierarchy.rb:17:in `run_before_each' rspec (1.3.0) lib/spec/example/example_methods.rb:107:in `run_before_each' rspec (1.3.0) lib/spec/example/example_methods.rb:128:in `before_each_example' rspec (1.3.0) lib/spec/example/example_methods.rb:39:in `execute' /usr/lib/ruby/1.8/timeout.rb:53:in `timeout' rspec (1.3.0) lib/spec/example/example_methods.rb:37:in `execute' rspec (1.3.0) lib/spec/example/example_group_methods.rb:214:in `run_examples' rspec (1.3.0) lib/spec/example/example_group_methods.rb:212:in `each' rspec (1.3.0) lib/spec/example/example_group_methods.rb:212:in `run_examples' rspec (1.3.0) lib/spec/example/example_group_methods.rb:103:in `run' rspec (1.3.0) lib/spec/runner/example_group_runner.rb:23:in `run' rspec (1.3.0) lib/spec/runner/example_group_runner.rb:22:in `each' rspec (1.3.0) lib/spec/runner/example_group_runner.rb:22:in `run' rspec (1.3.0) lib/spec/runner/options.rb:152:in `run_examples' rspec (1.3.0) lib/spec/runner/command_line.rb:9:in `run' rspec (1.3.0) bin/spec:5 /usr/bin/spec:19:in `load' /usr/bin/spec:19 From n00spam at comcast.net Tue Aug 9 19:23:37 2011 From: n00spam at comcast.net (Mike Jr) Date: Tue, 9 Aug 2011 16:23:37 -0700 (PDT) Subject: [rspec-users] Test execution time filtering Message-ID: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> As I understand it, RSpec runs in two passes. The first pass reads your code and invokes factory methods to generate instances of example- group and example subclasses. The second pass then invokes these generated instance to run your test. RSpec filters (via RSpec.configure) are set and operate during the first pass. The determination if a particular describe or it block is to be skipped is determined before the first test is executed in the second pass. Even if I were to use a lambda in the filter (see http://blog.davidchelimsky.net/2010/06/14/filtering-examples-in-rspec-2/ ) this would execute in the first pass. This is well and good. Why generate test code that will be skipped? But a sophisticated test will make decisions in mid test. If a certain test condition occurs, set a singleton hash and then have later tests condition their processing on that hash. In my tests, these if statements are within the it blocks so that they execute during the second pass. It sure would be less clutter if the describe and it statements supported a filter that is evaluated in the second pass. Or have I missed some existing feature? (Oh please be true!) --Thank you, --R.J. Ekim From todotresde at gmail.com Wed Aug 10 09:15:21 2011 From: todotresde at gmail.com (Leo) Date: Wed, 10 Aug 2011 06:15:21 -0700 (PDT) Subject: [rspec-users] How to detect test fail on after(:each) method? Message-ID: <5b538519-677d-4c86-8e2e-8f092d11512b@eb1g2000vbb.googlegroups.com> Hi All: I am working on a test, and I need to detect if the test failed on the after(:each) method, just to call a WebService. Thanks! From steven at snoble.net Wed Aug 10 17:53:37 2011 From: steven at snoble.net (steven_noble) Date: Wed, 10 Aug 2011 14:53:37 -0700 (PDT) Subject: [rspec-users] How to stop Rspec loading my Cucumber fixtures Message-ID: <9125868.241.1313013217319.JavaMail.geo-discussion-forums@prln29> I have a bunch of fixtures designed to populate the database for my Cucumber integration tests. But at this stage I want my database to be empty when using Rspec. So, I moved the fixtures from `/spec/fixtures` to `features/support/fixtures`, and updated `features/support/env.rb` to read: Fixtures.reset_cache fixtures_folder = File.join(RAILS_ROOT, 'features', 'support', 'fixtures') fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') } Fixtures.create_fixtures(fixtures_folder, fixtures) So far so good. Cucumber still loads the fixtures I moved as planned. But Rspec is still loading them too! This is despite `spec/spec_helper.rb` saying: config.fixture_path = "#{::Rails.root}/spec/fixtures" I have tried setting the following in `spec/spec_helper.rb`: config.use_transactional_fixtures = false However, it doesn't work. And it wouldn't be a long-term solution anyway, as I may need fixtures in Rspec in the future. How can I fix this? (Cross-posted from StackOverflow where there was no response.) -------------- next part -------------- An HTML attachment was scrubbed... URL: From chrismear at gmail.com Tue Aug 16 02:54:01 2011 From: chrismear at gmail.com (Chris Mear) Date: Tue, 16 Aug 2011 07:54:01 +0100 Subject: [rspec-users] Webrat and Rails 2.3.12 In-Reply-To: <3e373512-464f-4173-ac7e-e65d31b85fac@a4g2000yqg.googlegroups.com> References: <3e373512-464f-4173-ac7e-e65d31b85fac@a4g2000yqg.googlegroups.com> Message-ID: <4C6670F6-8296-4CDF-934B-C6EEAF34752C@gmail.com> On 9 Aug 2011, at 01:37, Matias wrote: > Im trying to integrate webrat in a rails 2.3.12: > > I get this OUTPUT: > 1) > NoMethodError in 'Campaign in first step testing request' > undefined method `visit' for > # > /home/matias/livechains/branches/test/livechains/spec/models/ > campaign_spec.rb:16: ... You're running a model spec here. A model spec is for testing a model class directly, not for simulating requests to your app. If you want to run requests, you need to be writing an integration spec. Chris From lists at ruby-forum.com Tue Aug 16 05:28:16 2011 From: lists at ruby-forum.com (Jigar P.) Date: Tue, 16 Aug 2011 11:28:16 +0200 Subject: [rspec-users] functional testing with(through?) chrome In-Reply-To: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> References: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> Message-ID: rspec + watir-webdriver (you do not need anything else) -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Tue Aug 16 05:33:45 2011 From: lists at ruby-forum.com (Jigar Patel) Date: Tue, 16 Aug 2011 11:33:45 +0200 Subject: [rspec-users] functional testing with(through?) chrome In-Reply-To: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> References: <9e34063ff1d7f5dc9164cc17a5bb76f6@ruby-forum.com> Message-ID: rspec + watir-webdriver -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Tue Aug 16 07:44:34 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 16 Aug 2011 06:44:34 -0500 Subject: [rspec-users] Test execution time filtering In-Reply-To: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> References: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> Message-ID: <4A015B01-4B2C-44D4-B8AB-B5B01EB8CBA8@gmail.com> On Aug 9, 2011, at 6:23 PM, Mike Jr wrote: > As I understand it, RSpec runs in two passes. The first pass reads > your code and invokes factory methods to generate instances of example- > group and example subclasses. The second pass then invokes these > generated instance to run your test. > > RSpec filters (via RSpec.configure) are set and operate during the > first pass. The determination if a particular describe or it block is > to be skipped is determined before the first test is executed in the > second pass. Even if I were to use a lambda in the filter (see > http://blog.davidchelimsky.net/2010/06/14/filtering-examples-in-rspec-2/ > ) this would execute in the first pass. > > This is well and good. Why generate test code that will be skipped? > > But a sophisticated test will make decisions in mid test. If a > certain test condition occurs, set a singleton hash and then have > later tests condition their processing on that hash. RSpec is built around the premise that each example is run in its own environment, and that one should not depend on the outcome of another. This is not unique to RSpec, btw. It's how all of the unit testing frameworks of which I am aware work. > In my tests, > these if statements are within the it blocks so that they execute > during the second pass. > > It sure would be less clutter if the describe and it statements > supported a filter that is evaluated in the second pass. > > Or have I missed some existing feature? (Oh please be true!) Nope. You have not missed something. You're just trying to put a square peg into a round hole. Cheers, David > --Thank you, > --R.J. Ekim From dchelimsky at gmail.com Tue Aug 16 07:49:49 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 16 Aug 2011 06:49:49 -0500 Subject: [rspec-users] How to stop Rspec loading my Cucumber fixtures In-Reply-To: <9125868.241.1313013217319.JavaMail.geo-discussion-forums@prln29> References: <9125868.241.1313013217319.JavaMail.geo-discussion-forums@prln29> Message-ID: <6E6C8395-3386-4B14-957E-E6DC29BBC3DE@gmail.com> On Aug 10, 2011, at 4:53 PM, steven_noble wrote: > I have a bunch of fixtures designed to populate the database for my Cucumber integration tests. > > But at this stage I want my database to be empty when using Rspec. > > So, I moved the fixtures from `/spec/fixtures` to `features/support/fixtures`, and updated `features/support/env.rb` to read: > > Fixtures.reset_cache > fixtures_folder = File.join(RAILS_ROOT, 'features', 'support', 'fixtures') > fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') } > Fixtures.create_fixtures(fixtures_folder, fixtures) > > So far so good. Cucumber still loads the fixtures I moved as planned. > > But Rspec is still loading them too! This is despite `spec/spec_helper.rb` saying: > > config.fixture_path = "#{::Rails.root}/spec/fixtures" > > I have tried setting the following in `spec/spec_helper.rb`: > > config.use_transactional_fixtures = false use_transactional_fixtures is a bit misleading: it's a hook from Rails that really means "run each example in a transaction." There is no way to globally turn fixtures on or off - you either declare fixtures (in which case the underlying framework will go look for them) or not (in which case it won't). > However, it doesn't work. And it wouldn't be a long-term solution anyway, as I may need fixtures in Rspec in the future. > > How can I fix this? How are you running rspec and cucumber? From dchelimsky at gmail.com Tue Aug 16 07:53:44 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 16 Aug 2011 06:53:44 -0500 Subject: [rspec-users] Specs doesn't run In-Reply-To: References: Message-ID: On Aug 6, 2011, at 11:50 AM, Matias wrote: > Rails 2.3.8 > > > GEMFILE > group :development do rspec needs to be in both the development and test groups. Change ^^ to: group :development, :test do > gem "rspec", "1.3.0" > gem "rspec-rails", "1.3.2" I'd also upgrade these to rspec-rails 1.3.4 and rspec 1.3.2, though I don't think that's related to this issue. HTH, David > gem "guard-rspec" #https://github.com/guard/guard-rspec > end > I run the specs with guard, spec spec/models/campaign_spec.rb or rake > spec:all but I don't get any output. From dchelimsky at gmail.com Tue Aug 16 07:58:01 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 16 Aug 2011 06:58:01 -0500 Subject: [rspec-users] How to detect test fail on after(:each) method? In-Reply-To: <5b538519-677d-4c86-8e2e-8f092d11512b@eb1g2000vbb.googlegroups.com> References: <5b538519-677d-4c86-8e2e-8f092d11512b@eb1g2000vbb.googlegroups.com> Message-ID: On Aug 10, 2011, at 8:15 AM, Leo wrote: > Hi All: > > I am working on a test, and I need to detect if the test failed on the > after(:each) method, just to call a WebService. You can't use after(:each) for this purpose because the status of each example isn't reported until the example and all of its hooks have run. Your best bet is a custom formatter: http://relishapp.com/rspec/rspec-core/docs/formatters/custom-formatters. HTH, David From dchelimsky at gmail.com Tue Aug 16 08:02:08 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 16 Aug 2011 07:02:08 -0500 Subject: [rspec-users] Stubbing #render_to_string overrides #render In-Reply-To: <8514586.484.1312817766637.JavaMail.geo-discussion-forums@vbhc7> References: <8514586.484.1312817766637.JavaMail.geo-discussion-forums@vbhc7> Message-ID: <09368582-4D57-4ECC-A8B4-F15E010D8A36@gmail.com> On Aug 8, 2011, at 10:36 AM, Nick wrote: > Hey folks. In my controller specs, when I stub #render_to_string, #render is also stubbed out. There is nothing in RSpec that binds these two methods together. > Thus, if a controller action calls #render, the stubbed #render returns what #render_to_string returns, which causes specs to fail. > > http://pastie.org/2339809 > > Am I doing something strange or wrong? If not, how can I stub #render_to_string without affecting #render ? IIRC, render(:json) eventually goes through render_to_string, which would explain the behavior you're seeing. I could be wrong, but have a look at the Rails code as a starting point. Cheers, David From lenny at aps.org Tue Aug 16 11:38:12 2011 From: lenny at aps.org (Lenny Marks) Date: Tue, 16 Aug 2011 11:38:12 -0400 Subject: [rspec-users] ControllerExampleGroup.bypass_rescue In-Reply-To: References: <670AF299-FCAF-4053-BF1A-628C7053F9CD@aps.org> <519BE192-A45E-4498-8A90-47D53B45A012@gmail.com> Message-ID: On Aug 11, 2011, at 8:59 AM, Justin Ko wrote: > > > On Thu, Aug 11, 2011 at 8:40 AM, David Chelimsky wrote: > On Aug 11, 2011, at 7:17 AM, Justin Ko wrote: > >> On Aug 10, 2011, at 11:13 AM, Lenny Marks wrote: >> >>> As best I can tell, bypass_rescue from rspec-rails-1 is no longer part of rspec-rails, '> 2'. I had been using it on occasion for things like: >>> >>> describe CorrespondencesController do >>> ... >>> describe '#show' do >>> it "should raise an AuthorizationError if current user is not the correspondent " do >>> bypass_rescue >>> ... >>> expect { do_get }.to raise_error(AuthorizationError) >>> >>> I know there are conflicting opinions on whether or not it's a good idea to directly check for exceptions this way, but I've always felt that this was appropriate for testing a controller action in isolation where the responsibility of the action under test was only to raise the error. >>> >>> Anyway, I couldn't find any recent references to this (not even in the rspec-rails repo). Is there any way to do this in rspec-rails-2 or is the official consensus to check only on response codes, etc... ? >>> >>> -lenny >> >> If you are rescuing an exception, test what the rescue does. Purposely cause the exception, then check the rescue does what it's supposed to. > > > Justin - that specifies how the controller handles exceptions, but not when they get raised, which is the purpose of bypass_rescue. In Lenny's example, above, the GET results in an AuthorizationError, but if that gets rescued then you have to specify it in terms of what the rescue does, not how it got to the rescue handler. That's probably OK in most cases, but there are cases where you have more than one way to get to a rescue, and you want to specify the details of how it gets there. > > Lenny, this was an oversight. Please submit an issue to http://github.com/rspec/rspec-rails/issues and we'll see about getting this reinstated. No guarantees since so much changed in Rails between v2 and v3, but if it's reasonable to add I think we should. > > Cheers, > David This has been taken care of. https://github.com/rspec/rspec-rails/issues/425 Thanks David. -lenny > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > "there are cases where you have more than one way to get to a rescue" > > Wouldn't you just mock/do-whatever to ensure the correct path to the rescue for the particular example? > _______________________________________________ > 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 lists at ruby-forum.com Tue Aug 16 12:30:43 2011 From: lists at ruby-forum.com (Jason R.) Date: Tue, 16 Aug 2011 18:30:43 +0200 Subject: [rspec-users] Rspec with ActionMailer and .deliver In-Reply-To: References: Message-ID: Chris M. wrote in post #1014770: >CurriculumCommentMailer.should_receive(:comment_update).and_return(double("mailer").stub(:deliver)) > > That's odd -- I would expect that second version to work if the first > version is working. What error did you get? Was it the same "undefined > method `deliver' for nil:NilClass" as before? It was giving this error: NoMethodError: undefined method `deliver' for # But, I changed the test to this and it works fine: CurriculumCommentMailer.should_receive(:comment_update).and_return(double("mailer", :deliver => true)) -Jason -- Posted via http://www.ruby-forum.com/. From nick at deadorange.com Tue Aug 16 14:51:11 2011 From: nick at deadorange.com (Nick) Date: Tue, 16 Aug 2011 11:51:11 -0700 (PDT) Subject: [rspec-users] Stubbing #render_to_string overrides #render In-Reply-To: <09368582-4D57-4ECC-A8B4-F15E010D8A36@gmail.com> References: <8514586.484.1312817766637.JavaMail.geo-discussion-forums@vbhc7> <09368582-4D57-4ECC-A8B4-F15E010D8A36@gmail.com> Message-ID: <19639300.1419.1313520671269.JavaMail.geo-discussion-forums@vbbdd1> On Tuesday, August 16, 2011 8:02:08 AM UTC-4, dchel... at gmail.com wrote: > > IIRC, render(:json) eventually goes through render_to_string, which would > explain the behavior you're seeing. I could be wrong, but have a look at the > Rails code as a starting point. Thanks, David. I'll trace through Rails' #render to see what's going on. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chrismear at gmail.com Tue Aug 16 18:18:02 2011 From: chrismear at gmail.com (Chris Mear) Date: Tue, 16 Aug 2011 23:18:02 +0100 Subject: [rspec-users] Rspec with ActionMailer and .deliver In-Reply-To: References: Message-ID: <85D8C860-790E-4E60-B4EB-364E0C560C55@gmail.com> On 16 Aug 2011, at 17:30, Jason R. wrote: > Chris M. wrote in post #1014770: >> CurriculumCommentMailer.should_receive(:comment_update).and_return(double("mailer").stub(:deliver)) >> >> That's odd -- I would expect that second version to work if the first >> version is working. What error did you get? Was it the same "undefined >> method `deliver' for nil:NilClass" as before? > > It was giving this error: > NoMethodError: undefined method `deliver' for > # > > But, I changed the test to this and it works fine: > CurriculumCommentMailer.should_receive(:comment_update).and_return(double("mailer", > :deliver => true)) Ah, of course! Because #stub doesn't return the object whose method you're stubbing, it returns a message expectation. I should have spotted that? Chris From stefan.kanev at gmail.com Wed Aug 17 03:59:50 2011 From: stefan.kanev at gmail.com (Stefan Kanev) Date: Wed, 17 Aug 2011 10:59:50 +0300 Subject: [rspec-users] Test execution time filtering In-Reply-To: <4A015B01-4B2C-44D4-B8AB-B5B01EB8CBA8@gmail.com> References: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> <4A015B01-4B2C-44D4-B8AB-B5B01EB8CBA8@gmail.com> Message-ID: > > RSpec is built around the premise that each example is run in its own > environment, and that one should not depend on the outcome of another. This > is not unique to RSpec, btw. It's how all of the unit testing frameworks of > which I am aware work. > I know I'm going off-topic, but TestNG supports that. You can say that one tests depends on another and use the second one to both set up the fixture and assert behavior. But I don't like it either, since it smells of Fragile Test. -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan.kanev at gmail.com Wed Aug 17 04:05:51 2011 From: stefan.kanev at gmail.com (Stefan Kanev) Date: Wed, 17 Aug 2011 11:05:51 +0300 Subject: [rspec-users] Test execution time filtering In-Reply-To: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> References: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> Message-ID: On Wed, Aug 10, 2011 at 2:23 AM, Mike Jr wrote: > But a sophisticated test will make decisions in mid test. If a > certain test condition occurs, set a singleton hash and then have > later tests condition their processing on that hash. In my tests, > these if statements are within the it blocks so that they execute > during the second pass. > > It sure would be less clutter if the describe and it statements > supported a filter that is evaluated in the second pass. > I am not sure if I understand you, but if I do, conventional wisdom suggests that having if statements in your tests is a very bad idea. You might want to check out this article: http://xunitpatterns.com/Conditional%20Test%20Logic.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From ckponnappa at gmail.com Wed Aug 17 06:29:35 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Wed, 17 Aug 2011 15:59:35 +0530 Subject: [rspec-users] Test execution time filtering In-Reply-To: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> References: <41df0f79-4cff-40f1-85f5-a0ea55cbb063@d32g2000yqa.googlegroups.com> Message-ID: > But a sophisticated test will make decisions in mid test. Quis custodiet ipsos custodes? You'll need to write specs for the logic in your specs then. Best, Sidu. http://c42.in http://blog.sidu.in On 10 August 2011 04:53, Mike Jr wrote: > As I understand it, RSpec runs in two passes. ?The first pass reads > your code and invokes factory methods to generate instances of example- > group and example subclasses. ?The second pass then invokes these > generated instance to run your test. > > RSpec filters (via RSpec.configure) are set and operate during the > first pass. ?The determination if a particular describe or it block is > to be skipped is determined before the first test is executed in the > second pass. ?Even if I were to use a lambda in the filter (see > http://blog.davidchelimsky.net/2010/06/14/filtering-examples-in-rspec-2/ > ) this would execute in the first pass. > > This is well and good. ?Why generate test code that will be skipped? > > But a sophisticated test will make decisions in mid test. ?If a > certain test condition occurs, set a singleton hash and then have > later tests condition their processing on that hash. ?In my tests, > these if statements are within the it blocks so that they execute > during the second pass. > > It sure would be less clutter if the describe and it statements > supported a filter that is evaluated in the second pass. > > Or have I missed some existing feature? (Oh please be true!) > > --Thank you, > --R.J. Ekim > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From ckponnappa at gmail.com Wed Aug 17 17:53:36 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Thu, 18 Aug 2011 03:23:36 +0530 Subject: [rspec-users] [ANN] rspec-http 0.9 with header matchers released Message-ID: Hello everyone, Someone I was pairing with this evening needed this for one of their projects, so I've added matchers for headers to rspec-http and pushed an updated gem. You can now do response.should have_header('Content-Type') response.should have_header('Content-Type' => 'application/json') response.should have_header('Content-Type' => /json/) More info at https://github.com/c42/rspec-http As always, I'd love to hear any feedback you may have. Best, Sidu. http://c42.in http://blog.sidu.in From lists at ruby-forum.com Thu Aug 18 10:33:34 2011 From: lists at ruby-forum.com (Antonov Alexei) Date: Thu, 18 Aug 2011 16:33:34 +0200 Subject: [rspec-users] [Cucumber] Cucumber::Ast::Table values dynamic modfication Message-ID: <391c066d50f752746c27ed17828e6309@ruby-forum.com> Hello there! We have a web table that has values generated on action date: If user logged in successfully today then we have on the web page: |action | user | date | comment | |log-in | user1 | 18/Aug/2011 | success | And we have corresponding cucumber check step: And action table is: |action | user | date | comment | |log-in | user1 | 18/Aug/2011 | success | The question is: how can i modify corresponding cucumber table object at step definition: And /^action table is:$/ do |expected_action_table| actual_action_table = get_action_table_from_ui expected_action_table.diff!(actual_action_table) end to use something like And action table is: |action | user | date | comment | |log-in | user1 | today | success | Where today will be converted to date and put into cucumber table object. So the date weill matched every date check is run. Thank you! -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Thu Aug 18 11:22:34 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 18 Aug 2011 10:22:34 -0500 Subject: [rspec-users] [Cucumber] Cucumber::Ast::Table values dynamic modfication In-Reply-To: <391c066d50f752746c27ed17828e6309@ruby-forum.com> References: <391c066d50f752746c27ed17828e6309@ruby-forum.com> Message-ID: On Aug 18, 2011, at 9:33 AM, Antonov Alexei wrote: > Hello there! > > We have a web table that has values generated on action date: > If user logged in successfully today then we have on the web page: > |action | user | date | comment | > |log-in | user1 | 18/Aug/2011 | success | > > And we have corresponding cucumber check step: > > And action table is: > |action | user | date | comment | > |log-in | user1 | 18/Aug/2011 | success | > > The question is: how can i modify corresponding cucumber table object at > step definition: > > And /^action table is:$/ do |expected_action_table| > actual_action_table = get_action_table_from_ui > expected_action_table.diff!(actual_action_table) > end > > to use something like > > And action table is: > |action | user | date | comment | > |log-in | user1 | today | success | > > > Where today will be converted to date and put into cucumber table > object. > So the date weill matched every date check is run. > > Thank you! Please send this to the Cucumber mailing list: http://groups.google.com/group/cukes Cheers, David From lists at ruby-forum.com Sun Aug 21 15:34:23 2011 From: lists at ruby-forum.com (Michael Hickman) Date: Sun, 21 Aug 2011 21:34:23 +0200 Subject: [rspec-users] problem with rspec test, undefined method 'post' Message-ID: <8c2e529943be341e605417ac05568d33@ruby-forum.com> I am writing a spec to test the behavior of the mashup_controller when someone sends a query through a URL. I need to simulate the parameters contained in the URL, and i read that the post() method will do that, however when i get an error: -------------------------------------------------------------------- 1) MashupController simulates query Failure/Error: post :create NoMethodError: undefined method `post' for # # ./mashup_controller_rspec.rb:9:in `block (2 levels) in ' Finished in 0.20199 seconds 1 example, 1 failure Failed examples: rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query -------------------------------------------------------------------- Here is my code: -------------------------------- require 'spec_helper' require 'mashup_controller.rb' describe MashupController do it "simulates query" do post :create end end -------------------------------- Sorry if I'm not making any sense. I am very new to rails and rspec. Any help would be appreciated. Thanks. -- Posted via http://www.ruby-forum.com/. From jko170 at gmail.com Sun Aug 21 19:27:44 2011 From: jko170 at gmail.com (Justin Ko) Date: Sun, 21 Aug 2011 17:27:44 -0600 Subject: [rspec-users] problem with rspec test, undefined method 'post' In-Reply-To: <8c2e529943be341e605417ac05568d33@ruby-forum.com> References: <8c2e529943be341e605417ac05568d33@ruby-forum.com> Message-ID: On Sun, Aug 21, 2011 at 1:34 PM, Michael Hickman wrote: > I am writing a spec to test the behavior of the mashup_controller when > someone sends a query through a URL. I need to simulate the parameters > contained in the URL, and i read that the post() method will do that, > however when i get an error: > -------------------------------------------------------------------- > 1) MashupController simulates query > Failure/Error: post :create > NoMethodError: > undefined method `post' for > # > # ./mashup_controller_rspec.rb:9:in `block (2 levels) in (required)>' > > Finished in 0.20199 seconds 1 example, 1 failure > > Failed examples: > > rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query > -------------------------------------------------------------------- > > > Here is my code: > > -------------------------------- > require 'spec_helper' > require 'mashup_controller.rb' > > describe MashupController do > it "simulates query" do > post :create > end > end > -------------------------------- > > Sorry if I'm not making any sense. I am very new to rails and rspec. Any > help would be appreciated. Thanks. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > Welcome to RSpec! You need to put your controller specs in a "spec/controllers" directory. RSpec will look for spec files in spec/controllers and include the necessary files in each spec file. Also, you do not need to "require 'mashup_controller.rb'" - Rails already loads it for you. Let us know if that works. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Sun Aug 21 22:11:55 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 21 Aug 2011 21:11:55 -0500 Subject: [rspec-users] ruby 1.8.6 Message-ID: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> Hey all, It's growing increasingly difficult for RSpec to support Ruby 1.8.6 as other libraries that rspec's development environment relies on drop support. Noting that 1.8.7 was released over three years ago (6/1/2008), I'd like to drop support for Ruby 1.8.6 for future versions of RSpec, but I'd like to hear from you before I cut the cord. Please raise your objections now or forever hold your peace. Cheers, David From luislavena at gmail.com Sun Aug 21 22:22:05 2011 From: luislavena at gmail.com (Luis Lavena) Date: Sun, 21 Aug 2011 23:22:05 -0300 Subject: [rspec-users] ruby 1.8.6 In-Reply-To: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> References: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> Message-ID: On Sun, Aug 21, 2011 at 11:11 PM, David Chelimsky wrote: > Hey all, > Hello David, > It's growing increasingly difficult for RSpec to support Ruby 1.8.6 as other libraries that rspec's development environment relies on drop support. Noting that 1.8.7 was released over three years ago (6/1/2008), I'd like to drop support for Ruby 1.8.6 for future versions of RSpec, but I'd like to hear from you before I cut the cord. > Good call. It is expected one last 1.8.6 release with bugfixes by the end of the year and will put the last stone (announced at RubyKaigi 2011 by Kirk Haines) >From RubyInstaller project we no longer promote it in the downloads section and we haven't perform any release of it since 1.8.6-p398 (p420 didn't got any release from us) If I might suggest something, please adjust the gemspec of all rspec2 gems on your next release to be >= 1.8.7, that way will block the installation of the gems reducing the potential bug reports due 1.8.6 behavior. Regards, -- Luis Lavena AREA 17 - Perfection in design is achieved not when there is nothing more to add, but rather when there is nothing more to take away. Antoine de Saint-Exup?ry From deliverable at gmail.com Sat Aug 20 21:53:59 2011 From: deliverable at gmail.com (braver) Date: Sat, 20 Aug 2011 18:53:59 -0700 (PDT) Subject: [rspec-users] inheriting test cases vs. mixing them in Message-ID: <8c976298-23a0-4ff1-8aec-b6455a2db444@a8g2000pro.googlegroups.com> I've come across an interesting blog post showing how to inherit tests: http://benbiddington.wordpress.com/2010/11/06/rspec-common-base-specs/ How does it stack against the usual mixing/shared_examples_for, beautifully described in http://blog.davidchelimsky.net/2010/11/07/specifying-mixins-with-shared-example-groups-in-rspec-2/ I.e., what are the cases where you'd do inheritance vs shared_examples_for? Cheers, Alexy From anexiole at gmail.com Mon Aug 22 02:14:36 2011 From: anexiole at gmail.com (ct9a) Date: Sun, 21 Aug 2011 23:14:36 -0700 (PDT) Subject: [rspec-users] Route is valid but not found :( Message-ID: Hi, guys, After reading the rspec book (dec 2010 edition), I went on to write controller specs for an application I'm porting over from rails 2.3.x to rails 3. 1) I ran 'rake routes' and got the following: parts GET /parts(.:format) {:action=>"index", :controller=>"parts"} POST /parts(.:format) {:action=>"create", :controller=>"parts"} new_part GET /parts/new(.:format) {:action=>"new", :controller=>"parts"} edit_part GET /parts/:id/edit(.:format) {:action=>"edit", :controller=>"parts"} part GET /parts/:id(.:format) {:action=>"show", :controller=>"parts"} PUT /parts/:id(.:format) {:action=>"update", :controller=>"parts"} DELETE /parts/:id(.:format) {:action=>"destroy", :controller=>"parts"} 2) I ran 'rake spec:controllers' and got the error below. Pending: PartsController the new part object's updates are saved successfully saves updates to a new part # Not Yet Implemented # ./spec/controllers/parts_controller_spec.rb:59 Failures: 1) PartsController saves updates to an existing part object successfully Failure/Error: put :update#, :part => { 'title' => 'Grimspeed' } ActionController::RoutingError: No route matches {:controller=>"parts", :action=>"update"} # ./spec/controllers/parts_controller_spec.rb:55 3) Here's what the spec reads: it 'saves updates to an existing part object successfully' do Part.should_receive(:update). with( 'title' => 'Brake pads' ). and_return(part) put :update#, :part => { 'title' => 'Brake pads' } end What I do not understand is that the :controller=>"parts", :action=>"update" route actually exists (when I ran "rake routes") but the tests does not acknowledge the existence of the 'update' method in the controller with PUT request. From jko170 at gmail.com Mon Aug 22 03:04:39 2011 From: jko170 at gmail.com (Justin Ko) Date: Mon, 22 Aug 2011 01:04:39 -0600 Subject: [rspec-users] inheriting test cases vs. mixing them in In-Reply-To: <8c976298-23a0-4ff1-8aec-b6455a2db444@a8g2000pro.googlegroups.com> References: <8c976298-23a0-4ff1-8aec-b6455a2db444@a8g2000pro.googlegroups.com> Message-ID: On Sat, Aug 20, 2011 at 7:53 PM, braver wrote: > I've come across an interesting blog post showing how to inherit > tests: > > http://benbiddington.wordpress.com/2010/11/06/rspec-common-base-specs/ > > How does it stack against the usual mixing/shared_examples_for, > beautifully described in > > > http://blog.davidchelimsky.net/2010/11/07/specifying-mixins-with-shared-example-groups-in-rspec-2/ > > I.e., what are the cases where you'd do inheritance vs > shared_examples_for? > > Cheers, > Alexy > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > I see no use case for class inheritance in RSpec. If you need to include some methods in a spec, use the `include` config option: module MySpecMethods def foo end end RSpec.configure do |config| config.include MySpecMethods # to restrict to controllers, for example config.include MySpecMethods, :type => :controller end -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Mon Aug 22 03:14:14 2011 From: jko170 at gmail.com (Justin Ko) Date: Mon, 22 Aug 2011 01:14:14 -0600 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: Message-ID: On Mon, Aug 22, 2011 at 12:14 AM, ct9a wrote: > Hi, guys, > > After reading the rspec book (dec 2010 edition), I went on to write > controller specs for an application I'm porting over from rails 2.3.x > to rails 3. > > > 1) I ran 'rake routes' and got the following: > > parts GET /parts(.:format) > {:action=>"index", :controller=>"parts"} > POST /parts(.:format) > {:action=>"create", :controller=>"parts"} > new_part GET /parts/new(.:format) > {:action=>"new", :controller=>"parts"} > edit_part GET /parts/:id/edit(.:format) > {:action=>"edit", :controller=>"parts"} > part GET /parts/:id(.:format) > {:action=>"show", :controller=>"parts"} > PUT /parts/:id(.:format) > The :id means you need to pass an "id" to the route helper method. > {:action=>"update", :controller=>"parts"} > DELETE /parts/:id(.:format) > {:action=>"destroy", :controller=>"parts"} > > 2) I ran 'rake spec:controllers' and got the error below. > > Pending: > PartsController the new part object's updates are saved successfully > saves updates to a new part > # Not Yet Implemented > # ./spec/controllers/parts_controller_spec.rb:59 > > Failures: > > 1) PartsController saves updates to an existing part object > successfully > Failure/Error: put :update#, :part => { 'title' => 'Grimspeed' } > ActionController::RoutingError: > No route matches {:controller=>"parts", :action=>"update"} > # ./spec/controllers/parts_controller_spec.rb:55 > > 3) Here's what the spec reads: > > it 'saves updates to an existing part object successfully' do > Part.should_receive(:update). > with( 'title' => 'Brake pads' ). > and_return(part) > put :update#, :part => { 'title' => 'Brake pads' } > end > So you need to "find" the part first: part = double('part') Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update).with('title' => 'Brake pads').and_return(part) put :update, :id => 1, :part => {'title' => 'Brake pads'} > > > > > What I do not understand is that > the :controller=>"parts", :action=>"update" route actually exists > (when I ran "rake routes") but the tests does not acknowledge the > existence of the 'update' method in the controller with PUT request. > You need to pass in the :id for Rails to recognize the route. > > > > > > > > > _______________________________________________ > 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 ckponnappa at gmail.com Mon Aug 22 04:36:40 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Mon, 22 Aug 2011 14:06:40 +0530 Subject: [rspec-users] ruby 1.8.6 In-Reply-To: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> References: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> Message-ID: Hi David, Sounds good to me. I haven't seen a 1.8.6 project since December last year. Best, Sidu. http://c42.in http://blog.sidu.in On 22 August 2011 07:41, David Chelimsky wrote: > Hey all, > > It's growing increasingly difficult for RSpec to support Ruby 1.8.6 as other libraries that rspec's development environment relies on drop support. Noting that 1.8.7 was released over three years ago (6/1/2008), I'd like to drop support for Ruby 1.8.6 for future versions of RSpec, but I'd like to hear from you before I cut the cord. > > Please raise your objections now or forever hold your peace. > > Cheers, > David > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From forum at erisiandiscord.de Mon Aug 22 06:11:18 2011 From: forum at erisiandiscord.de (Nikolay Sturm) Date: Mon, 22 Aug 2011 12:11:18 +0200 Subject: [rspec-users] mock weirdness Message-ID: <20110822101118.GB17356@erisiandiscord.de> Hi, I have a strange problem with mocking an object that has a method called 'load'. With Rails 2.3 and rspec-rails 1.3 I could do sth like this: describe Foo do let(:bar) { mock(Bar).as_null_object } before(:each) do Bar.stub(:new).and_return(bar) end it 'does something' do Foo.do_something end end with class Foo def do_something bar = Bar.new bar.load end end After an upgrade to rails 3.0.10 and rspec-rails 2.6.1 I get an ArgumentError: wrong number of arguments (0 for 1) If I add "bar.method(:load)" to do_something(), it prints # which doesn't look right to me. I am using ree-1.8.7-2011.03. Does anyone have an idea what this is about and how to properly deal with this? cheers, Nikolay -- "It's all part of my Can't-Do approach to life." Wally From dchelimsky at gmail.com Mon Aug 22 09:08:05 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 22 Aug 2011 08:08:05 -0500 Subject: [rspec-users] mock weirdness In-Reply-To: <20110822101118.GB17356@erisiandiscord.de> References: <20110822101118.GB17356@erisiandiscord.de> Message-ID: <5D7C300B-9665-4A9B-827B-D883DC8446C1@gmail.com> On Aug 22, 2011, at 5:11 AM, Nikolay Sturm wrote: > Hi, > > I have a strange problem with mocking an object that has a method called > 'load'. With Rails 2.3 and rspec-rails 1.3 I could do sth like this: > > describe Foo do > let(:bar) { mock(Bar).as_null_object } > before(:each) do > Bar.stub(:new).and_return(bar) > end > > it 'does something' do > Foo.do_something > end > end > > with > > class Foo > def do_something > bar = Bar.new > bar.load > end > end > > After an upgrade to rails 3.0.10 and rspec-rails 2.6.1 I get an > ArgumentError: > wrong number of arguments (0 for 1) > > If I add "bar.method(:load)" to do_something(), it prints > > # This ^^ tells you everything you need to know to research the problem. If you look at the code for ActiveSupport::Dependencies (https://github.com/rails/rails/blob/v3.0.10/activesupport/lib/active_support/dependencies.rb), you'll see that the Loadable module is included in every object. See: https://github.com/rails/rails/blob/v3.0.10/activesupport/lib/active_support/dependencies.rb#L654 https://github.com/rails/rails/blob/v3.0.10/activesupport/lib/active_support/dependencies.rb#L281-286 This means that even mock objects already have a load method, which is defined to accept one or more arguments: https://github.com/rails/rails/blob/v3.0.10/activesupport/lib/active_support/dependencies.rb#L234-236 This means that you need to explicitly stub the load method to do what you're trying to do: let(:bar) { mock(Bar, :load => nil).as_null_object } HTH, David > which doesn't look right to me. I am using ree-1.8.7-2011.03. > > Does anyone have an idea what this is about and how to properly deal > with this? > > cheers, > > Nikolay From forum at erisiandiscord.de Mon Aug 22 10:11:54 2011 From: forum at erisiandiscord.de (Nikolay Sturm) Date: Mon, 22 Aug 2011 16:11:54 +0200 Subject: [rspec-users] mock weirdness In-Reply-To: <5D7C300B-9665-4A9B-827B-D883DC8446C1@gmail.com> References: <20110822101118.GB17356@erisiandiscord.de> <5D7C300B-9665-4A9B-827B-D883DC8446C1@gmail.com> Message-ID: <20110822141154.GB18839@erisiandiscord.de> * David Chelimsky [2011-08-22]: > This means that you need to explicitly stub the load method to do what > you're trying to do: > > let(:bar) { mock(Bar, :load => nil).as_null_object } Thanks for the explanation, David. I wasn't sure whether or not this was accepted behaviour, I would have expected a null object to be, well, an object without methods. At least the next guy's google search will point to the solution. :) cheers, Nikolay From cremes.devlist at mac.com Mon Aug 22 09:10:44 2011 From: cremes.devlist at mac.com (Chuck Remes) Date: Mon, 22 Aug 2011 08:10:44 -0500 Subject: [rspec-users] ruby 1.8.6 In-Reply-To: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> References: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> Message-ID: <72A7EC6D-96C9-4513-B778-593A88C5A70E@mac.com> On Aug 21, 2011, at 9:11 PM, David Chelimsky wrote: > Hey all, > > It's growing increasingly difficult for RSpec to support Ruby 1.8.6 as other libraries that rspec's development environment relies on drop support. Noting that 1.8.7 was released over three years ago (6/1/2008), I'd like to drop support for Ruby 1.8.6 for future versions of RSpec, but I'd like to hear from you before I cut the cord. > > Please raise your objections now or forever hold your peace. All of my legacy projects and new projects are 1.9-only, so there is no objection from me. cr From Rob.aldred at stardotstar.com Mon Aug 22 10:35:51 2011 From: Rob.aldred at stardotstar.com (Rob Aldred) Date: Mon, 22 Aug 2011 15:35:51 +0100 Subject: [rspec-users] ruby 1.8.6 In-Reply-To: <72A7EC6D-96C9-4513-B778-593A88C5A70E@mac.com> References: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> <72A7EC6D-96C9-4513-B778-593A88C5A70E@mac.com> Message-ID: On 22 Aug 2011, at 14:10, Chuck Remes wrote: > On Aug 21, 2011, at 9:11 PM, David Chelimsky wrote: > >> Hey all, >> >> It's growing increasingly difficult for RSpec to support Ruby 1.8.6 as other libraries that rspec's development environment relies on drop support. Noting that 1.8.7 was released over three years ago (6/1/2008), I'd like to drop support for Ruby 1.8.6 for future versions of RSpec, but I'd like to hear from you before I cut the cord. >> >> Please raise your objections now or forever hold your peace. > > All of my legacy projects and new projects are 1.9-only, so there is no objection from me. > > cr > Agree, ditch it. As you pointed out David, it's 3 years old. People will have to stick of older versions of rspec if they cannot move from 1.8.6 I'd imagine they are the minority. -- @robaldred From lenny at aps.org Mon Aug 22 17:50:10 2011 From: lenny at aps.org (Lenny Marks) Date: Mon, 22 Aug 2011 17:50:10 -0400 Subject: [rspec-users] [JRuby] simulating a java exception in a spec Message-ID: <042D20E6-3CE3-4888-93CE-B369724704A6@aps.org> JRuby 1.6.2 rspec-core (2.6.4) rspec-expectations (2.6.0) rspec-mocks (2.6.0) rspec-rails (2.6.1) I'm sure this has more to do with the way JRuby wraps Java exceptions but I figured I'd post here in case anyone here has any insight or pointers. In the context of writing a spec for a model like thing that wraps legacy Java code, I found myself attempting to stub a method on a Java Exception rescued in the implementation. eg. describe '#valid?' do .... it 'adds validation exceptions raised by service to #errors' do ve = ValidationException #a java exception ve.stub(:localized_message).and_return('a bunch of errors') service.stub(:validateTaskForSave).and_raise(ve) subject.valid? subject.errors.should == ['a bunch of errors'] end the above example fails because the :localize_message stub is ignored and instead the real implementation receives the message. I know this typically screams typo but not in this case. Here is a more boiled down version: specify 'rescued exception message should be "bar" because I stubbed it' do begin e = Java::java.lang.NullPointerException.new('foo') e.stub(:message).and_return('bar') raise e rescue Java::java.lang.NullPointerException => e e.message.should == 'bar' end end Failure/Error: e.message.should == 'bar' expected: "bar" got: "foo" (using ==) That seemed odd to me but maybe moot anyway since in reality I would be need to rescue a NativeException masquerading as my target exception. e.g. This code def valid? begin service.validateTaskForSave(task) rescue ValidationException => e puts "rescued exception: #{e.class.name}" ..... prints "rescued exception: NativeException" when hit through the console. So how would one simulate a NativeException if needed (i.e. you want to stub methods on it)? -lenny From deliverable at gmail.com Mon Aug 22 21:33:38 2011 From: deliverable at gmail.com (Alexy Khrabrov) Date: Mon, 22 Aug 2011 18:33:38 -0700 Subject: [rspec-users] inheriting test cases vs. mixing them in Message-ID: I've come across an interesting blog post showing how to inherit tests: http://benbiddington.wordpress.com/2010/11/06/rspec-common-base-specs/ How does it stack against the usual mixing/shared_examples_for, beautifully described in http://blog.davidchelimsky.net/2010/11/07/specifying-mixins-with-shared-example-groups-in-rspec-2/ I.e., what are the cases where you'd do inheritance vs. the usual shared_examples_for? Cheers, Alexy From anexiole at gmail.com Mon Aug 22 23:09:23 2011 From: anexiole at gmail.com (ct9a) Date: Mon, 22 Aug 2011 20:09:23 -0700 (PDT) Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: Message-ID: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> Thanks, Justin. I have the part object mocked up before each spec runs. ------- Extract begins ----------------- let(:part){ mock_model('Part').as_null_object } before do Part.stub(:new).and_return(part) end ------- Extract ends ----------------- With that in mind, I just made changes to my spec as below: ------- Extract begins ----------------- it 'saves updates to an existing part object successfully' do Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update).with( 'title' => 'Grimspeed' ).and_return(part) put :update, :id => 1, :part => { 'title' => 'Grimspeed' } end ------- Extract ends ----------------- When I run the 'rake spec:controllers', I get the following error: ------- Error extract begins ----------------- 1) PartsController saves updates to an existing part object successfully Failure/Error: Part.should_receive(:update).with( 'title' => 'Brake pads' ).and_return(part) ().update({"title"=>"Brake pads"}) expected: 1 time received: 0 times # ./spec/controllers/parts_controller_spec.rb:53 ------- Error extract ends ----------------- I then put in Justin's change with the use of double() as follows: ------- Source code extract begins ----------------- it 'saves updates to an existing part object successfully' do part = double(part) Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update).with( 'title' => 'Brake pads' ).and_return(part) put :update, :id => 1, :part => { 'title' => 'Brake pads' } end ------- Source code extract ends ----------------- and I got the following error: ------- Error extract begins ----------------- 1) PartsController saves updates to an existing part object successfully Failure/Error: put :update, :id => 1, :part => { 'title' => 'Brake pads' } Double received unexpected message :update_attributes with ({"title"=>"Brake pads"}) # ./app/controllers/parts_controller.rb:63:in `update' # ./app/controllers/parts_controller.rb:62:in `update' # ./spec/controllers/parts_controller_spec.rb:55 ------- Error extract ends ----------------- I have also looked at a few other posts in the group but can't figure out what's causing update_attributes not to be recognised :( - http://groups.google.com/group/rspec/browse_thread/thread/9bfb4a348799c30f/30de6b7953222b5a?lnk=gst&q=update_attributes#30de6b7953222b5a - http://groups.google.com/group/rspec/browse_thread/thread/fe4985e2b8be16da/f590a9e0510ff4fb?lnk=gst&q=update_attributes#f590a9e0510ff4fb My update message in the parts controller reads as below: ------- Source code extract starts ----------------- def update @part = Part.find(params[:id]) respond_to do |format| if @part.update_attributes(params[:part]) format.html { redirect_to(@part, :notice => 'Part was successfully updated.') } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @part.errors, :status => :unprocessable_entity } end end end ------- Source code extract ends ----------------- I have used this bit of code without problems in the past. update_attributes, like save, is part of the ActiveRecord api (http:// ar.rubyonrails.org/). Well, given that controllers are not to know of the implementation of messages, I have then stubbed the message to the update_attributes method in the parts controller. ------- Source code extract starts ----------------- context 'saves updates to an existing part object successfully' do before do part = double('part').stub(:update_attributes).and_return(true) end it 'does its job in saving the update' do Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update).with('title' => 'Brake pads').and_return(part) put :update, :id => 1, :part => {'title' => 'Brake pads'} end end ------- Source code extract ends ----------------- Alas, I got the error below :( ------- Error extract starts ----------------- 1) PartsController saves updates to an existing part object successfully does its job in saving the update Failure/Error: Part.should_receive(:update).with('title' => 'Brake pads').and_return(part) ().update({"title"=>"Brake pads"}) expected: 1 time received: 0 times # ./spec/controllers/parts_controller_spec.rb:61 ------- Error extract ends ----------------- From jko170 at gmail.com Mon Aug 22 23:55:44 2011 From: jko170 at gmail.com (Justin Ko) Date: Mon, 22 Aug 2011 21:55:44 -0600 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> Message-ID: On Mon, Aug 22, 2011 at 9:09 PM, ct9a wrote: > > > Thanks, Justin. > > I have the part object mocked up before each spec runs. > > ------- Extract begins ----------------- > > let(:part){ > mock_model('Part').as_null_object > } > > before do > Part.stub(:new).and_return(part) > end > > ------- Extract ends ----------------- > > > With that in mind, I just made changes to my spec as below: > > ------- Extract begins ----------------- > > it 'saves updates to an existing part object successfully' do > Part.should_receive(:find).with(1).and_return(part) > Part.should_receive(:update).with( 'title' => > 'Grimspeed' ).and_return(part) > Should be: Part.should_receive(:update_attributes).with('title' => 'Grimspeed').and_return(part) No where do you call `update` on Part. > put :update, :id => 1, :part => { 'title' => 'Grimspeed' } > end > > ------- Extract ends ----------------- > > > When I run the 'rake spec:controllers', I get the following error: > > > ------- Error extract begins ----------------- > > 1) PartsController saves updates to an existing part object > successfully > Failure/Error: Part.should_receive(:update).with( 'title' => > 'Brake pads' ).and_return(part) > ( created_by: string, updated_by: string, created_at: datetime, > updated_at: datetime) (class)>).update({"title"=>"Brake pads"}) > expected: 1 time > received: 0 times > # ./spec/controllers/parts_controller_spec.rb:53 > > ------- Error extract ends ----------------- > > I then put in Justin's change with the use of double() as follows: > > ------- Source code extract begins ----------------- > > it 'saves updates to an existing part object successfully' do > part = double(part) > Part.should_receive(:find).with(1).and_return(part) > Part.should_receive(:update).with( 'title' => 'Brake > pads' ).and_return(part) > put :update, :id => 1, :part => { 'title' => 'Brake pads' } > end > > ------- Source code extract ends ----------------- > > and I got the following error: > > ------- Error extract begins ----------------- > > 1) PartsController saves updates to an existing part object > successfully > Failure/Error: put :update, :id => 1, :part => { 'title' => > 'Brake pads' } > Double received unexpected message :update_attributes with > ({"title"=>"Brake pads"}) > # ./app/controllers/parts_controller.rb:63:in `update' > # ./app/controllers/parts_controller.rb:62:in `update' > # ./spec/controllers/parts_controller_spec.rb:55 > > ------- Error extract ends ----------------- > > I have also looked at a few other posts in the group but can't figure > out what's causing > update_attributes not to be recognised :( > > - > http://groups.google.com/group/rspec/browse_thread/thread/9bfb4a348799c30f/30de6b7953222b5a?lnk=gst&q=update_attributes#30de6b7953222b5a > - > http://groups.google.com/group/rspec/browse_thread/thread/fe4985e2b8be16da/f590a9e0510ff4fb?lnk=gst&q=update_attributes#f590a9e0510ff4fb > > My update message in the parts controller reads as below: > > ------- Source code extract starts ----------------- > def update > @part = Part.find(params[:id]) > > respond_to do |format| > if @part.update_attributes(params[:part]) > format.html { redirect_to(@part, :notice => 'Part was > successfully updated.') } > format.xml { head :ok } > else > format.html { render :action => "edit" } > format.xml { render :xml => @part.errors, :status > => :unprocessable_entity } > end > end > end > > ------- Source code extract ends ----------------- > > > I have used this bit of code without problems in the past. > update_attributes, like save, is part of the ActiveRecord api (http:// > ar.rubyonrails.org/). > > Well, given that controllers are not to know of the implementation of > messages, > I have then stubbed the message to the update_attributes method in the > parts controller. > > ------- Source code extract starts ----------------- > context 'saves updates to an existing part object successfully' do > before do > part = > double('part').stub(:update_attributes).and_return(true) > end > > it 'does its job in saving the update' do > Part.should_receive(:find).with(1).and_return(part) > Part.should_receive(:update).with('title' => 'Brake > pads').and_return(part) > put :update, :id => 1, :part => {'title' => 'Brake pads'} > end > end > ------- Source code extract ends ----------------- > > Alas, I got the error below :( > > ------- Error extract starts ----------------- > > 1) PartsController saves updates to an existing part object > successfully does its job in saving the update > Failure/Error: Part.should_receive(:update).with('title' => > 'Brake pads').and_return(part) > ( created_by: string, updated_by: string, created_at: datetime, > updated_at: datetime) (class)>).update({"title"=>"Brake pads"}) > expected: 1 time > received: 0 times > # ./spec/controllers/parts_controller_spec.rb:61 > > ------- Error extract ends ----------------- > _______________________________________________ > 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 anexiole at gmail.com Tue Aug 23 00:14:57 2011 From: anexiole at gmail.com (ct9a) Date: Mon, 22 Aug 2011 21:14:57 -0700 (PDT) Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> Message-ID: On Aug 23, 1:55?pm, Justin Ko wrote: > On Mon, Aug 22, 2011 at 9:09 PM, ct9a wrote: > > > Thanks, Justin. > > > I have the part object mocked up before each spec runs. > > > ------- Extract begins ----------------- > > > ? ?let(:part){ > > ? ? ? ?mock_model('Part').as_null_object > > ? ?} > > > ? ?before do > > ? ? ? ?Part.stub(:new).and_return(part) > > ? ?end > > > ------- Extract ends ----------------- > > > With that in mind, I just made changes to my spec as below: > > > ------- Extract begins ----------------- > > > ? ?it 'saves updates to an existing part object successfully' do > > ? ? ? ? Part.should_receive(:find).with(1).and_return(part) > > ? ? ? ?Part.should_receive(:update).with( 'title' => > > 'Grimspeed' ).and_return(part) > > Should be: > > Part.should_receive(:update_attributes).with('title' => > 'Grimspeed').and_return(part) > > No where do you call `update` on Part. Yes, I have done that and the spec looks like below. ------------------ Spec extract begins ------------------------------------------ context 'saves updates to an existing part object successfully' do it 'does its job in saving the update' do Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part) put :update, :id => 1, :part => {'title' => 'Brake pads'} end end ------------------ Spec extract ends ------------------------------------------ Alas, it's still giving out an error indicating the update_attributes message is never called. I've already got a put request to the 'update' method with an id and the part hash parameter and yet it looks like 'update' is not called (which in will call update_attributes in it). ----------------- Error extract starts ------------------------------------------------- 1) PartsController saves updates to an existing part object successfully does its job in saving the update Failure/Error: Part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part) ().update_attributes({"title"=>"Brake pads"}) expected: 1 time received: 0 times # ./spec/controllers/parts_controller_spec.rb:54 Finished in 0.25483 seconds 8 examples, 1 failure, 1 pending ----------------- Error extract end ------------------------------------------------- Hmm.... What is missing? From lists at ruby-forum.com Tue Aug 23 01:24:59 2011 From: lists at ruby-forum.com (Nishith R.) Date: Tue, 23 Aug 2011 07:24:59 +0200 Subject: [rspec-users] Cucumber: Running a single feature / scenario In-Reply-To: <9AA10895-5708-4DBD-A807-AA7F81ED2BE8@mattwynne.net> References: <9AA10895-5708-4DBD-A807-AA7F81ED2BE8@mattwynne.net> Message-ID: For sake of completness, if you are using bundle in rails3, and don't want to invoke rake [to save init time], you can also execute it the following way: bundle exec cucumber --guess --profile default path/feature_name.feature:line_number HTH Nishith -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Tue Aug 23 01:25:09 2011 From: lists at ruby-forum.com (Nishith R.) Date: Tue, 23 Aug 2011 07:25:09 +0200 Subject: [rspec-users] Cucumber: Running a single feature / scenario In-Reply-To: <9AA10895-5708-4DBD-A807-AA7F81ED2BE8@mattwynne.net> References: <9AA10895-5708-4DBD-A807-AA7F81ED2BE8@mattwynne.net> Message-ID: For sake of completeness, if you are using bundle in rails3, and don't want to invoke rake [to save init time], you can also execute it the following way: bundle exec cucumber --guess --profile default path/feature_name.feature:line_number HTH Nishith -- Posted via http://www.ruby-forum.com/. From jko170 at gmail.com Tue Aug 23 07:54:59 2011 From: jko170 at gmail.com (Justin Ko) Date: Tue, 23 Aug 2011 05:54:59 -0600 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> Message-ID: <7B7E5E9E-3D18-43D4-8BDD-2DDF706F695C@gmail.com> Sent from my iPhone On Aug 22, 2011, at 10:14 PM, ct9a wrote: > > > On Aug 23, 1:55 pm, Justin Ko wrote: >> On Mon, Aug 22, 2011 at 9:09 PM, ct9a wrote: >> >>> Thanks, Justin. >> >>> I have the part object mocked up before each spec runs. >> >>> ------- Extract begins ----------------- >> >>> let(:part){ >>> mock_model('Part').as_null_object >>> } >> >>> before do >>> Part.stub(:new).and_return(part) >>> end >> >>> ------- Extract ends ----------------- >> >>> With that in mind, I just made changes to my spec as below: >> >>> ------- Extract begins ----------------- >> >>> it 'saves updates to an existing part object successfully' do >>> Part.should_receive(:find).with(1).and_return(part) >>> Part.should_receive(:update).with( 'title' => >>> 'Grimspeed' ).and_return(part) >> >> Should be: >> >> Part.should_receive(:update_attributes).with('title' => >> 'Grimspeed').and_return(part) >> >> No where do you call `update` on Part. > > Yes, I have done that and the spec looks like below. > > ------------------ Spec extract begins > ------------------------------------------ > context 'saves updates to an existing part object successfully' do > it 'does its job in saving the update' do > Part.should_receive(:find).with(1).and_return(part) > Part.should_receive(:update_attributes).with('title' => > 'Brake pads').and_return(part) > put :update, :id => 1, :part => {'title' => 'Brake pads'} > end > end > ------------------ Spec extract ends > ------------------------------------------ > > Alas, it's still giving out an error indicating the update_attributes > message is never called. > I've already got a put request to the 'update' method with an id and > the part hash parameter > and yet it looks like 'update' is not called (which in will call > update_attributes in it). > > ----------------- Error extract starts > ------------------------------------------------- > > 1) PartsController saves updates to an existing part object > successfully does its job in saving the update > Failure/Error: > Part.should_receive(:update_attributes).with('title' => 'Brake > pads').and_return(part) > ( created_by: string, updated_by: string, created_at: datetime, > updated_at: datetime) (class)>).update_attributes({"title"=>"Brake > pads"}) > expected: 1 time > received: 0 times > # ./spec/controllers/parts_controller_spec.rb:54 > > Finished in 0.25483 seconds > 8 examples, 1 failure, 1 pending > > ----------------- Error extract end > ------------------------------------------------- > > Hmm.... What is missing? Do you have a before filter somewhere that is preventing the :update_attributes message from being received? > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From jko170 at gmail.com Tue Aug 23 07:57:25 2011 From: jko170 at gmail.com (Justin Ko) Date: Tue, 23 Aug 2011 05:57:25 -0600 Subject: [rspec-users] Cucumber: Running a single feature / scenario In-Reply-To: References: <9AA10895-5708-4DBD-A807-AA7F81ED2BE8@mattwynne.net> Message-ID: Sent from my iPhone On Aug 22, 2011, at 11:25 PM, "Nishith R." wrote: > For sake of completeness, if you are using bundle in rails3, and don't > want to invoke rake [to save init time], you can also execute it the > following way: > > bundle exec cucumber --guess --profile default > path/feature_name.feature:line_number > > HTH > Nishith > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Cucumber has its own mailing list. From anexiole at gmail.com Tue Aug 23 08:11:51 2011 From: anexiole at gmail.com (Gordon Yeong) Date: Tue, 23 Aug 2011 22:11:51 +1000 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: <7B7E5E9E-3D18-43D4-8BDD-2DDF706F695C@gmail.com> References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> <7B7E5E9E-3D18-43D4-8BDD-2DDF706F695C@gmail.com> Message-ID: > > > Do you have a before filter somewhere that is preventing the > :update_attributes message from being received? > > I have checked my application's controllers (app/controllers/application_controller.rb and app/controllers/parts_controller.rb) and controller spec (spec/controllers/parts_controller_spec.rb) and found no before filter that is preventing the :update_attributes message from being received by Parts. Below is what my spec/controllers/parts_controller_spec.rb looks like: ------- Source code, spec/controllers/parts_controller_spec.rb starts ----------------------- require 'spec_helper' describe PartsController do let(:part){ mock_model('Part').as_null_object } before do Part.stub(:new).and_return(part) end it "creates a new part" do Part.should_receive(:new). with('title' => 'HKS boost controller'). and_return(part) post :create, :part => { 'title' => 'HKS boost controller' } end context "saves the new part object successfully" do it "sets the flash with a success message" do post :create flash[:notice].should eq('Part was successfully created.') end it "redirects the user back to the Parts index page" do post :create response.should redirect_to( :action => 'index' ) end end context "the new part object fails to save" do before do # sabotage and make the save fail part.stub(:save).and_return(false) # call the create method which will have save in its process post :create end it "assigns @part with the data from db (or a blank one if it's the first time" do assigns[:part].should eq(part) end it "renders the 'new' template again" do # test that the template, 'new' is rendered. Of course, for this # work, the part's attribute variable values must be passed to the # view for rails to successfully render the template response.should render_template('new') end end context 'saves updates to an existing part object successfully' do it 'does its job in saving the update' do Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part) put :update, :id => 1, :part => {'title' => 'Brake pads'} flash[:notice].should eq('Part was successfully updated.') end end end ------- Source code, spec/controllers/parts_controller_spec.rb ends ----------------------- Do you have any good tutorials which illustrate the update message which I could refer to? I've been digging but can't find anything helpful :( thanks for your help :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From anexiole at gmail.com Tue Aug 23 08:22:11 2011 From: anexiole at gmail.com (Gordon Yeong) Date: Tue, 23 Aug 2011 22:22:11 +1000 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> <7B7E5E9E-3D18-43D4-8BDD-2DDF706F695C@gmail.com> Message-ID: I found out why it was not working. The line, 'Part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part)' should not be there because the controller specs should not care about implementation (ie. how things are processed, rather just what is done). I realised this when I looked at my specs for creation of new objects. I commented the line and the spec now passes as expected. ------------ Spec extract starts ------------------------- context 'saves updates to an existing part object successfully' do it 'does its job in saving the update' do Part.should_receive(:find).with(1).and_return(part) # Part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part) put :update, :id => 1, :part => {'title' => 'Brake pads'} flash[:notice].should eq('Part was successfully updated.') end end ------------ Spec extract ends ------------------------- Thank you for your help, Justin! Gordon Yeong :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From ken.chien at gmail.com Tue Aug 23 08:19:10 2011 From: ken.chien at gmail.com (Ken Chien) Date: Tue, 23 Aug 2011 08:19:10 -0400 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> Message-ID: I think you were closer when you had: ------- Source code extract starts ----------------- context 'saves updates to an existing part object successfully' do before do part = double('part').stub(:update_ attributes).and_return(true) end it 'does its job in saving the update' do Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update).with('title' => 'Brake pads').and_return(part) put :update, :id => 1, :part => {'title' => 'Brake pads'} end end ------- Source code extract ends ----------------- However, you were setting part equal to the return value from "stub(:update_attributes).and_return(true)" which returns a Proc, not a double. Instead, try this: let(:part) { double('part') } it 'does its job in saving the update' do part.stub(:update_attributes).and_return(true) # <--- moved this from the before method Part.should_receive(:find).with(1).and_return(part) Part.should_receive(:update).with('title' => 'Brake pads').and_return(part) put :update, :id => 1, :part => {'title' => 'Brake pads'} end end -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Tue Aug 23 08:46:23 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 23 Aug 2011 07:46:23 -0500 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> <7B7E5E9E-3D18-43D4-8BDD-2DDF706F695C@gmail.com> Message-ID: On Aug 23, 2011, at 7:22 AM, Gordon Yeong wrote: > I found out why it was not working. > > The line, 'Part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part)' should not be there because the controller specs should not care about implementation (ie. how things are processed, rather just what is done). I realised this when I looked at my specs for creation of new objects. > > I commented the line and the spec now passes as expected. > > > ------------ Spec extract starts ------------------------- > > context 'saves updates to an existing part object successfully' do > it 'does its job in saving the update' do > Part.should_receive(:find).with(1).and_return(part) > # Part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part) ^^ Change the class name Part to the variable part ^^: part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(part) It's the part object, not the Part class that will receive update_attributes. HTH, David > put :update, :id => 1, :part => {'title' => 'Brake pads'} > flash[:notice].should eq('Part was successfully updated.') > end > end > > ------------ Spec extract ends ------------------------- > > Thank you for your help, Justin! > > Gordon Yeong :) > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Cheers, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Tue Aug 23 08:46:35 2011 From: jko170 at gmail.com (Justin Ko) Date: Tue, 23 Aug 2011 06:46:35 -0600 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> <7B7E5E9E-3D18-43D4-8BDD-2DDF706F695C@gmail.com> Message-ID: On Tue, Aug 23, 2011 at 6:22 AM, Gordon Yeong wrote: > I found out why it was not working. > > The line, 'Part.should_receive(:update_attributes).with('title' => 'Brake > pads').and_return(part)' should not be there because the controller specs > should not care about implementation (ie. how things are processed, rather > just what is done). > Depends on what you want to do. Now, your controller spec is coupled to your model, so if "update_attributes" fails, your controller spec will fail too. > I realised this when I looked at my specs for creation of new objects. > > I commented the line and the spec now passes as expected. > > > ------------ Spec extract starts ------------------------- > > context 'saves updates to an existing part object successfully' do > it 'does its job in saving the update' do > Part.should_receive(:find).with(1).and_return(part) > # Part.should_receive(:update_attributes).with('title' => 'Brake > pads').and_return(part) > put :update, :id => 1, :part => {'title' => 'Brake pads'} > flash[:notice].should eq('Part was successfully updated.') > end > end > > ------------ Spec extract ends ------------------------- > > Thank you for your help, Justin! > > Gordon Yeong :) > > _______________________________________________ > 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 jko170 at gmail.com Tue Aug 23 08:50:14 2011 From: jko170 at gmail.com (Justin Ko) Date: Tue, 23 Aug 2011 06:50:14 -0600 Subject: [rspec-users] Route is valid but not found :( In-Reply-To: References: <7e25c758-c10a-4539-9851-1fcfc13a7777@t20g2000prf.googlegroups.com> <7B7E5E9E-3D18-43D4-8BDD-2DDF706F695C@gmail.com> Message-ID: On Tue, Aug 23, 2011 at 6:11 AM, Gordon Yeong wrote: > >> Do you have a before filter somewhere that is preventing the >> :update_attributes message from being received? >> >> I have checked my application's controllers > (app/controllers/application_controller.rb and > app/controllers/parts_controller.rb) and controller spec > (spec/controllers/parts_controller_spec.rb) and found no before filter that > is preventing the :update_attributes message from being received by Parts. > > Below is what my spec/controllers/parts_controller_spec.rb looks like: > > ------- Source code, spec/controllers/parts_controller_spec.rb starts > ----------------------- > > require 'spec_helper' > > describe PartsController do > let(:part){ > mock_model('Part').as_null_object > } > > before do > Part.stub(:new).and_return(part) > end > > it "creates a new part" do > Part.should_receive(:new). > with('title' => 'HKS boost controller'). > and_return(part) > post :create, :part => { 'title' => 'HKS boost controller' } > end > > context "saves the new part object successfully" do > it "sets the flash with a success message" do > post :create > flash[:notice].should eq('Part was successfully created.') > end > > it "redirects the user back to the Parts index page" do > post :create > response.should redirect_to( :action => 'index' ) > end > end > > context "the new part object fails to save" do > before do > # sabotage and make the save fail > part.stub(:save).and_return(false) > # call the create method which will have save in its process > post :create > end > > it "assigns @part with the data from db (or a blank one if it's the > first time" do > assigns[:part].should eq(part) > end > > it "renders the 'new' template again" do > # test that the template, 'new' is rendered. Of course, for > this > # work, the part's attribute variable values must be passed to > the > # view for rails to successfully render the template > response.should render_template('new') > end > end > > context 'saves updates to an existing part object successfully' do > it 'does its job in saving the update' do > Part.should_receive(:find).with(1).and_return(part) > Part.should_receive(:update_attributes).with('title' => 'Brake > pads').and_return(part) > Oh duh. You're calling `should_receive(:update_attributes)` on `Part` when it should be called on `part`: part.should_receive(:update_attributes).with('title' => 'Brake pads').and_return(true) > put :update, :id => 1, :part => {'title' => 'Brake pads'} > flash[:notice].should eq('Part was successfully updated.') > end > end > > end > > ------- Source code, spec/controllers/parts_controller_spec.rb ends > ----------------------- > > Do you have any good tutorials which illustrate the update message which I > could refer to? > I've been digging but can't find anything helpful :( > > thanks for your help :) > > > > _______________________________________________ > 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 lists at ruby-forum.com Tue Aug 23 13:20:40 2011 From: lists at ruby-forum.com (Vito Botta) Date: Tue, 23 Aug 2011 19:20:40 +0200 Subject: [rspec-users] Rails validation spec fails (but code works in development env) In-Reply-To: <0b8fb34e2bf66fb3234379560c3e2dac@ruby-forum.com> References: <0b8fb34e2bf66fb3234379560c3e2dac@ruby-forum.com> Message-ID: Hi there, di you ever find the answer? I am having the same issue now, really weird. -- Posted via http://www.ruby-forum.com/. From ckponnappa at gmail.com Tue Aug 23 13:29:02 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Tue, 23 Aug 2011 22:59:02 +0530 Subject: [rspec-users] [JRuby] simulating a java exception in a spec In-Reply-To: <042D20E6-3CE3-4888-93CE-B369724704A6@aps.org> References: <042D20E6-3CE3-4888-93CE-B369724704A6@aps.org> Message-ID: it 'adds validation exceptions raised by service to #errors' do ve = ValidationException #a java exception ve.stub(:localized_message).and_return('a bunch of errors') .... Just clarifying, but did you mean ve = ValidationException.new I tried replicating your spec but with java.lang.RuntimeException instead of ValidationException and I got exception class/object expected because raise expects an instance of the exception, not the exception class. Am I missing something? Best, Sidu. http://c42.in http://blog.sidu.in On 23 August 2011 03:20, Lenny Marks wrote: > service.stub(:validateTaskForSave).and_raise(ve) From lists at ruby-forum.com Tue Aug 23 13:47:22 2011 From: lists at ruby-forum.com (Roger Pack) Date: Tue, 23 Aug 2011 19:47:22 +0200 Subject: [rspec-users] pretty inspect for diff? Message-ID: <2438fabd8f30e749a36ef2bdf5909910@ruby-forum.com> Hello. I noticed this test "failed" message: https://gist.github.com/1165975 The salient part is this diff: Diff: @@ -1,4 +1,4 @@ -{"blank_outs"=>[["0:00:56.0", "0:00:57.0"], ["0:01:05", "0:01:14.500"]], +{"imdb_id"=>"tt1727587", "mutes"=>[], - "imdb_id"=>"t1727587"} + "blank_outs"=>[["0:00:56", "0:00:57"], ["0:01:05", "0:01:14.500"]]} I believe that maybe could have been made to be more useful if it had been a diff of the "pretty_print" version of these two constructs. Cheers! -roger- -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Tue Aug 23 13:48:07 2011 From: lists at ruby-forum.com (Roger Pack) Date: Tue, 23 Aug 2011 19:48:07 +0200 Subject: [rspec-users] ruby 1.8.6 In-Reply-To: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> References: <5DE5941F-E033-4A45-9F9C-B22907095E3B@gmail.com> Message-ID: <699dd4d049fbd72e7aaa7d8702c40e1b@ruby-forum.com> > It's growing increasingly difficult for RSpec to support Ruby 1.8.6 as > other libraries that rspec's development environment relies on drop > support. Noting that 1.8.7 was released over three years ago (6/1/2008), > I'd like to drop support for Ruby 1.8.6 for future versions of RSpec, > but I'd like to hear from you before I cut the cord. As long as it supports 1.8.7 I'm good, and others who are using 1.8.6 can "hopefully" upgrade to 1.8.7 or just use some old version of rspec that works there still... -roger- -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Tue Aug 23 14:00:44 2011 From: lists at ruby-forum.com (Roger Pack) Date: Tue, 23 Aug 2011 20:00:44 +0200 Subject: [rspec-users] slightly confusing error message Message-ID: <8f0ef6b409bc7413d021896c0b2ca43c@ruby-forum.com> This error message: it 'should' do proc { eval("a=")}.should raise_exception(/SyntaxError/) end expected Exception with message matching /SyntaxError/, got # Is a bit confusing, since it appears from the error output that it *does* match /SyntaxError/ Might be able to word it better or along those lines. Thanks! -roger- -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Tue Aug 23 14:10:45 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 23 Aug 2011 13:10:45 -0500 Subject: [rspec-users] pretty inspect for diff? In-Reply-To: <2438fabd8f30e749a36ef2bdf5909910@ruby-forum.com> References: <2438fabd8f30e749a36ef2bdf5909910@ruby-forum.com> Message-ID: On Aug 23, 2011, at 12:47 PM, Roger Pack wrote: > Hello. > > I noticed this test "failed" message: > > https://gist.github.com/1165975 > > The salient part is this diff: > > Diff: > > @@ -1,4 +1,4 @@ > -{"blank_outs"=>[["0:00:56.0", "0:00:57.0"], ["0:01:05", > "0:01:14.500"]], > +{"imdb_id"=>"tt1727587", > "mutes"=>[], > - "imdb_id"=>"t1727587"} > + "blank_outs"=>[["0:00:56", "0:00:57"], ["0:01:05", > "0:01:14.500"]]} > > > I believe that maybe could have been made to be more useful if it had > been a diff of the "pretty_print" version of these two constructs. > > Cheers! > -roger- Please post feature requests to the issue trackers: http://github.com/rspec/rspec-expectations/issues in this case. Much easier to keep track of everything that way. Cheers, David From lenny at aps.org Tue Aug 23 14:27:14 2011 From: lenny at aps.org (Lenny Marks) Date: Tue, 23 Aug 2011 14:27:14 -0400 Subject: [rspec-users] [JRuby] simulating a java exception in a spec In-Reply-To: <042D20E6-3CE3-4888-93CE-B369724704A6@aps.org> References: <042D20E6-3CE3-4888-93CE-B369724704A6@aps.org> Message-ID: <4E521029-6269-47C4-9B92-0DCF64D1124E@aps.org> > > On Aug 22, 2011, at 5:50 PM, Lenny Marks wrote: > >> JRuby 1.6.2 >> rspec-core (2.6.4) >> rspec-expectations (2.6.0) >> rspec-mocks (2.6.0) >> rspec-rails (2.6.1) >> >> I'm sure this has more to do with the way JRuby wraps Java exceptions but I figured I'd post here in case anyone here has any insight or pointers. In the context of writing a spec for a model like thing that wraps legacy Java code, I found myself attempting to stub a method on a Java Exception rescued in the implementation. >> >> eg. >> >> describe '#valid?' do >> .... >> it 'adds validation exceptions raised by service to #errors' do >> ve = ValidationException #a java exception >> ve.stub(:localized_message).and_return('a bunch of errors') >> >> service.stub(:validateTaskForSave).and_raise(ve) >> >> subject.valid? >> >> subject.errors.should == ['a bunch of errors'] >> end > it 'adds validation exceptions raised by service to #errors' do > ve = ValidationException #a java exception > ve.stub(:localized_message).and_return('a bunch of errors') > .... > > > Just clarifying, but did you mean > ve = ValidationException.new > > > I tried replicating your spec but with java.lang.RuntimeException > instead of ValidationException and I got > exception class/object expected > > because raise expects an instance of the exception, not the exception > class. Am I missing something? > > Best, > Sidu. > http://c42.in > http://blog.sidu.in You have to raise an exception instance, not the class of the exception as is typical with Ruby exceptions. Check out the "boiled down" example below to reproduce. >> >> the above example fails because the :localize_message stub is ignored and instead the real implementation receives the message. I know this typically screams typo but not in this case. Here is a more boiled down version: >> >> specify 'rescued exception message should be "bar" because I stubbed it' do >> begin >> e = Java::java.lang.NullPointerException.new('foo') >> e.stub(:message).and_return('bar') >> raise e >> rescue Java::java.lang.NullPointerException => e >> e.message.should == 'bar' >> end >> end >> >> Failure/Error: e.message.should == 'bar' >> expected: "bar" >> got: "foo" (using ==) >> >> That seemed odd to me but maybe moot anyway since in reality I would be need to rescue a NativeException masquerading as my target exception. >> >> e.g. This code >> >> def valid? >> begin >> service.validateTaskForSave(task) >> rescue ValidationException => e >> puts "rescued exception: #{e.class.name}" >> ..... >> >> prints "rescued exception: NativeException" when hit through the console. The more I think about this, the more I wish it was different(more explicit) in JRuby. IMO, the above is very unintuitive (i.e. if I rescue a specific exception class then I would expect the exception instance to be an instance of that class). Any attempt to reference a custom method on a rescued java exception results in "undefined method". rescue MyJavaException => e # e.some_method "undefined method" e.cause.some_method # need to unwrap end AFAICT, the JRuby wiki makes no mention of this behavior ( https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby ). Of course that's more suited for the JRuby mailing list, but if it is just a matter of filling in the docs then there should still be an easy way to simulate such an exception with #and_raise such that custom methods on the exception can be stubbed. No?? -lenny >> >> So how would one simulate a NativeException if needed (i.e. you want to stub methods on it)? >> >> -lenny >> >> >> >> >> >> _______________________________________________ >> 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 ckponnappa at gmail.com Tue Aug 23 14:43:11 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Wed, 24 Aug 2011 00:13:11 +0530 Subject: [rspec-users] [JRuby] simulating a java exception in a spec In-Reply-To: References: <042D20E6-3CE3-4888-93CE-B369724704A6@aps.org> Message-ID: Please ignore my previous question - I just realised you're doing Java::java.lang.NullPointerException.new in your second example. I'm seeing the same behaviour and can't think of any way around it off the top of my head. You could try redefining Java::JavaLang::NullPointerException to be a Ruby RuntimeError within the scope of your spec but I'm not sure how this will work (if at all) when Java::JavaLang::NullPointerException is raised from native code. Best, Sidu. http://c42.in On 23 August 2011 22:59, Sidu Ponnappa wrote: > it 'adds validation exceptions raised by service to #errors' do > ? ? ve = ValidationException #a java exception > ? ? ve.stub(:localized_message).and_return('a bunch of errors') > ? ?.... > > > Just clarifying, but did you mean > ? ?ve = ValidationException.new > > > I tried replicating your spec but with java.lang.RuntimeException > instead of ValidationException and I got > ? exception class/object expected > > because raise expects an instance of the exception, not the exception > class. Am I missing something? > > Best, > Sidu. > http://c42.in > http://blog.sidu.in > > > > On 23 August 2011 03:20, Lenny Marks wrote: >> service.stub(:validateTaskForSave).and_raise(ve) > From dchelimsky at gmail.com Tue Aug 23 15:16:49 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 23 Aug 2011 14:16:49 -0500 Subject: [rspec-users] slightly confusing error message In-Reply-To: <8f0ef6b409bc7413d021896c0b2ca43c@ruby-forum.com> References: <8f0ef6b409bc7413d021896c0b2ca43c@ruby-forum.com> Message-ID: <7382813B-5CCD-4B34-B2A6-82FC99334C78@gmail.com> On Aug 23, 2011, at 1:00 PM, Roger Pack wrote: > This error message: > > > it 'should' do > proc { eval("a=")}.should raise_exception(/SyntaxError/) > end > > > expected Exception with message matching /SyntaxError/, got > # > > Is a bit confusing, since it appears from the error output that it > *does* match /SyntaxError/ > > Might be able to word it better or along those lines. > Thanks! > -roger- Please report this to http://github.com/rspec/rspec-expectations/issues. Cheers, David From Rob at AgileConsultingLLC.com Tue Aug 23 16:26:38 2011 From: Rob at AgileConsultingLLC.com (Rob Biedenharn) Date: Tue, 23 Aug 2011 16:26:38 -0400 Subject: [rspec-users] slightly confusing error message In-Reply-To: <7382813B-5CCD-4B34-B2A6-82FC99334C78@gmail.com> References: <8f0ef6b409bc7413d021896c0b2ca43c@ruby-forum.com> <7382813B-5CCD-4B34-B2A6-82FC99334C78@gmail.com> Message-ID: <74181EFC-2D9C-4877-8285-DB0F4076EC44@AgileConsultingLLC.com> On Aug 23, 2011, at 3:16 PM, David Chelimsky wrote: > On Aug 23, 2011, at 1:00 PM, Roger Pack wrote: > >> This error message: >> >> >> it 'should' do >> proc { eval("a=")}.should raise_exception(/SyntaxError/) >> end shouldn't that be raise_exception(SyntaxError) that is, the exception class, not a Regexp literal to match its name. >> >> >> expected Exception with message matching /SyntaxError/, got >> # >> >> Is a bit confusing, since it appears from the error output that it >> *does* match /SyntaxError/ >> >> Might be able to word it better or along those lines. >> Thanks! >> -roger- > > Please report this to http://github.com/rspec/rspec-expectations/issues > . > > Cheers, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Rob Biedenharn Rob at AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab at GaslightSoftware.com http://GaslightSoftware.com/ From dchelimsky at gmail.com Tue Aug 23 17:18:48 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 23 Aug 2011 16:18:48 -0500 Subject: [rspec-users] slightly confusing error message In-Reply-To: <74181EFC-2D9C-4877-8285-DB0F4076EC44@AgileConsultingLLC.com> References: <8f0ef6b409bc7413d021896c0b2ca43c@ruby-forum.com> <7382813B-5CCD-4B34-B2A6-82FC99334C78@gmail.com> <74181EFC-2D9C-4877-8285-DB0F4076EC44@AgileConsultingLLC.com> Message-ID: On Aug 23, 2011, at 3:26 PM, Rob Biedenharn wrote: > On Aug 23, 2011, at 3:16 PM, David Chelimsky wrote: > >> On Aug 23, 2011, at 1:00 PM, Roger Pack wrote: >> >>> This error message: >>> >>> >>> it 'should' do >>> proc { eval("a=")}.should raise_exception(/SyntaxError/) >>> end > > shouldn't that be > raise_exception(SyntaxError) > that is, the exception class, not a Regexp literal to match its name. It supports any of: raise_error(ErrorClass) raise_error(ErrorClass, string) raise_error(ErrorClass, /regex/) raise_error(string) raise_error(/regex/) (or raise_exception with the same set of alternatives) Cheers, David > >>> >>> >>> expected Exception with message matching /SyntaxError/, got >>> # >>> >>> Is a bit confusing, since it appears from the error output that it >>> *does* match /SyntaxError/ >>> >>> Might be able to word it better or along those lines. >>> Thanks! >>> -roger- >> >> Please report this to http://github.com/rspec/rspec-expectations/issues. >> >> Cheers, >> David >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > Rob Biedenharn > Rob at AgileConsultingLLC.com http://AgileConsultingLLC.com/ > rab at GaslightSoftware.com http://GaslightSoftware.com/ > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From Rob at AgileConsultingLLC.com Wed Aug 24 12:48:24 2011 From: Rob at AgileConsultingLLC.com (Rob Biedenharn) Date: Wed, 24 Aug 2011 12:48:24 -0400 Subject: [rspec-users] slightly confusing error message In-Reply-To: References: <8f0ef6b409bc7413d021896c0b2ca43c@ruby-forum.com> <7382813B-5CCD-4B34-B2A6-82FC99334C78@gmail.com> <74181EFC-2D9C-4877-8285-DB0F4076EC44@AgileConsultingLLC.com> Message-ID: On Aug 23, 2011, at 5:18 PM, David Chelimsky wrote: > > On Aug 23, 2011, at 3:26 PM, Rob Biedenharn wrote: > >> On Aug 23, 2011, at 3:16 PM, David Chelimsky wrote: >> >>> On Aug 23, 2011, at 1:00 PM, Roger Pack wrote: >>> >>>> This error message: >>>> >>>> >>>> it 'should' do >>>> proc { eval("a=")}.should raise_exception(/SyntaxError/) >>>> end >> >> shouldn't that be >> raise_exception(SyntaxError) >> that is, the exception class, not a Regexp literal to match its name. > > It supports any of: > > raise_error(ErrorClass) > raise_error(ErrorClass, string) > raise_error(ErrorClass, /regex/) > raise_error(string) > raise_error(/regex/) > > (or raise_exception with the same set of alternatives) > > Cheers, > David RSpec 1.3 (from apidock) raise_exception(exception=Exception, message=nil, &block) public With no args, matches if any exception is raised. With a named exception, matches only if that specific exception is raised. With a named exception and messsage specified as a String, matches only if bothmatch. With a named exception and messsage specified as a Regexp, matches only if both match. Passan optional block to perform extra verifications on the exception matched Rspec 2 (from http://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/raise-error-matcher) only shows forms with either nothing specified, or the first argument is always the Exception class. If you use one of: raise_error(string) raise_error(/regex/) against what is the string or /regex/ matched? The message associated to the Exception or the NAME of the exception. >>>> expected Exception with message matching /SyntaxError/, got >>>> # >>>> >>>> Is a bit confusing, since it appears from the error output that it >>>> *does* match /SyntaxError/ The MESSAGE does not match /SyntaxError/ but it would have matched / syntax error/. I think that's where the OP is confused. -Rob >>>> >>>> Might be able to word it better or along those lines. >>>> Thanks! >>>> -roger- >>> >>> Please report this to http://github.com/rspec/rspec-expectations/issues >>> . >>> >>> Cheers, >>> David >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> Rob Biedenharn >> Rob at AgileConsultingLLC.com http://AgileConsultingLLC.com/ >> rab at GaslightSoftware.com http://GaslightSoftware.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 Rob Biedenharn Rob at AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab at GaslightSoftware.com http://GaslightSoftware.com/ From matthiassiegel at gmail.com Wed Aug 24 20:40:13 2011 From: matthiassiegel at gmail.com (Matthias Siegel) Date: Thu, 25 Aug 2011 10:10:13 +0930 Subject: [rspec-users] Tests that require a logged in user / session cookie Message-ID: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> Hi, I'm fairly new to RSpec with Rails and I'm trying to work out how I can write request specs for resources that require a logged in user. I can't get this one to pass: describe "GET /admin/account" do it "should have a 200 status code when logged in" do post "/login", { :email => @user.email, :password => @user.password } response.should redirect_to("/admin") response.code.should eq("302") get "/admin/account" response.code.should eq("200") end end The login post part works fine and the session gets created correctly in the login method, but then the test fails at 'get "/admin/account"' because the session suddenly is empty. I have tried another approach where I set the session manually, to simulate a logged in user: describe "GET /admin/account" do it "should have a 200 status code when logged in" do session[:user_id] ||= @user.id get "/admin/account" response.code.should eq("200") end end But again the session arrives empty in my authorisation method when trying 'get "/admin/account"'. My guess is that it fails because the session relies on cookies and in test mode there obviously is no browser and no cookie. Are there ways to simulate a logged in user in an app that creates sessions with cookies? Thanks for any suggestions From jko170 at gmail.com Wed Aug 24 21:40:49 2011 From: jko170 at gmail.com (Justin Ko) Date: Wed, 24 Aug 2011 19:40:49 -0600 Subject: [rspec-users] Tests that require a logged in user / session cookie In-Reply-To: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> References: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> Message-ID: On Wed, Aug 24, 2011 at 6:40 PM, Matthias Siegel wrote: > Hi, > > I'm fairly new to RSpec with Rails and I'm trying to work out how I can > write request specs for resources that require a logged in user. > > I can't get this one to pass: > > > describe "GET /admin/account" do > > it "should have a 200 status code when logged in" do > post "/login", { :email => @user.email, :password => @user.password } > response.should redirect_to("/admin") > response.code.should eq("302") > get "/admin/account" > response.code.should eq("200") > end > > end > > > The login post part works fine and the session gets created correctly in > the login method, but then the test fails at 'get "/admin/account"' because > the session suddenly is empty. > > I have tried another approach where I set the session manually, to simulate > a logged in user: > > > describe "GET /admin/account" do > > it "should have a 200 status code when logged in" do > session[:user_id] ||= @user.id > get "/admin/account" > response.code.should eq("200") > end > > end > > > But again the session arrives empty in my authorisation method when trying > 'get "/admin/account"'. > > My guess is that it fails because the session relies on cookies and in test > mode there obviously is no browser and no cookie. > Are there ways to simulate a logged in user in an app that creates sessions > with cookies? > > Thanks for any suggestions > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > What you are doing *should* work. Are there any before_filters altering the session? Maybe a gem doing it? Maybe you have an admin namespace that calls/uses a different session? -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Thu Aug 25 04:14:58 2011 From: lists at ruby-forum.com (Andrei Ursan) Date: Thu, 25 Aug 2011 10:14:58 +0200 Subject: [rspec-users] How should I interpret RCov, code coverage? Message-ID: First of all I'm new to RCov. I'm having a rails application with around 20 models and 20 controllers + helpers and others. And I've got an unusual good RCov test coverage, 39%(total) and 31%(code coverage) - and this with only 12 RSpec examples. I'm running RCov with the following options: t.rcov_opts = "--callsites --xrefs --no-comments --rails --exclude test/*,spec/*,features/*,factories/*,gems/*" What kind of heuristic is RCov using? I'm reading its manual http://www.linuxcertif.com/man/1/rcov/ and it says: > rcov is a code coverage tool for Ruby. It creates code coverage > reports showing the unit test coverage of the target code. > > rcov does "statement coverage", also referred to as "C0 coverage analysis". It > tests, if each line of the source code has been executed. > > rcov is typically used to find the areas of a program that have not > been sufficiently tested. It reports, what code has not been run by > any test cases. That being said, it means that: (in my case) 31% Lines Of Code of the application logic have been executed by the test files. E.g. if a method has been executed for one test case than that method (the LOC that represent that method) are 100% tested. The bad side is that if a test file loads a Class A (just by including its name into its logic), and doesn't execute any of its method, the methods signatures of that class will count as executed LOC 100% tested, therefore the number can grow quite easy. **Right?** Is an RCov code coverage of 100% really good? Because in my opinion a method should be tested for more than one case but rcov doesn't care about this :(. Is there another tool which does a better job on rails projects than RCov on test coverage? -- Posted via http://www.ruby-forum.com/. From ckponnappa at gmail.com Thu Aug 25 06:12:08 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Thu, 25 Aug 2011 15:42:08 +0530 Subject: [rspec-users] How should I interpret RCov, code coverage? In-Reply-To: References: Message-ID: > Is an RCov code coverage of 100% really good? Because in my opinion a > method should be tested for more than one case but rcov doesn't care > about this :(. RCov is C0 coverage[1]. It's trivial to hit >95% coverage; in fact you can very quickly achieve >60% coverage by writing a handful of Cukes on most (simple/new) Rails apps. > And I've got an unusual good RCov test coverage, 39%(total) and 31%(code > coverage) - and this with only 12 RSpec examples. With C0, this isn't unusual. To mitigate this effect, on rails projects we typically have two coverage report builds - just model specs, model + controller specs. I typically expect my codebases to have ~95% coverage for the model build, somewhat higher from the model + controller build. I don't usually look at coverage from Cukes as it doesn't really say very much. > the methods signatures of that class will count as executed LOC > 100% tested, therefore the number can grow quite easy. **Right?** It's not unusual for us to see codebases where the customer mandated a certain coverage number, but the contractor was unfamiliar with TDD and so simply wrote specs with no assertions. The coverage numbers are met, but the specs are useless. You can mitigate this to some extent with a library like heckle, but YMMV. > Is there another tool which does a better job on rails projects than > RCov on test coverage? AFAIK, Ruby tools only provides C0 coverage metrics. Best, Sidu. http://c42.in [1] http://grosser.it/2008/04/04/whats-my-coverage-c0-c1-c2-c3-path-coverage/ On 25 August 2011 13:44, Andrei Ursan wrote: > First of all I'm new to RCov. > > I'm having a rails application with around 20 models and 20 controllers > + helpers and others. > > And I've got an unusual good RCov test coverage, 39%(total) and 31%(code > coverage) - and this with only 12 RSpec examples. > > I'm running RCov with the following options: > > ?t.rcov_opts ?= "--callsites --xrefs ?--no-comments --rails --exclude > test/*,spec/*,features/*,factories/*,gems/*" > > What kind of heuristic is RCov using? I'm reading its manual > http://www.linuxcertif.com/man/1/rcov/ and it says: > >> rcov is a code coverage tool for Ruby. It creates code coverage >> reports showing the unit test coverage of the target code. >> >> rcov does "statement coverage", also referred to as "C0 coverage analysis". It >> tests, if each line of the source code has been executed. >> >> rcov is typically used to find the areas of a program that have not >> been sufficiently tested. It reports, what code has not been run by >> any test cases. > > That being said, it means that: > > (in my case) 31% ?Lines Of Code of the application logic have been > executed by the test files. E.g. if a method has been executed for one > test case than that method (the LOC that represent that method) are 100% > tested. The bad side is that if a test file loads a Class A (just by > including its name into its logic), and doesn't execute any of its > method, the methods signatures of that class will count as executed LOC > 100% tested, therefore the number can grow quite easy. **Right?** > > > Is an RCov code coverage of 100% really good? Because in my opinion a > method should be tested for more than one case but rcov doesn't care > about this :(. > > Is there another tool which does a better job on rails projects than > RCov on test coverage? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From seanliugm at gmail.com Tue Aug 23 06:02:27 2011 From: seanliugm at gmail.com (foreverman) Date: Tue, 23 Aug 2011 03:02:27 -0700 (PDT) Subject: [rspec-users] doesn't rspec check the existence of template file Message-ID: <3ea58df2-7ccd-4e22-9f2d-df9518de838f@y4g2000vbx.googlegroups.com> Hey, I am using rspec (1.3.0) and rspec-rails (1.3.2) for my current rails2 project, I found that in my controller test, rspec doesn't check the existence of view template. For example, it "should render show template" do get :show response.should render_template :show end The above controller test will pass even the 'show' template doesn't exist. I am curious if this is a bug or I did something wrong. Thanks. From dchelimsky at gmail.com Thu Aug 25 08:04:38 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 25 Aug 2011 07:04:38 -0500 Subject: [rspec-users] doesn't rspec check the existence of template file In-Reply-To: <3ea58df2-7ccd-4e22-9f2d-df9518de838f@y4g2000vbx.googlegroups.com> References: <3ea58df2-7ccd-4e22-9f2d-df9518de838f@y4g2000vbx.googlegroups.com> Message-ID: <68DACD7E-FE22-4631-90E4-C02F42F9A370@gmail.com> On Aug 23, 2011, at 5:02 AM, foreverman wrote: > Hey, > > I am using rspec (1.3.0) and rspec-rails (1.3.2) for my current rails2 > project, I found that in my controller test, rspec doesn't check the > existence of view template. For example, > it "should render show template" do > get :show > response.should render_template :show > end > > The above controller test will pass even the 'show' template doesn't > exist. > > I am curious if this is a bug or I did something wrong. Neither. There's a bit of history here so bear with me. The names used in Rails for the different kinds of tests it offers do not align with the names we used to use before Rails came along. Here are some older definitions (which are the ones to which I subscribe, but that doesn't mean "they are the right ones"): Unit test: specifies behavior of an object in isolation. Integration test: specifies behavior of two or more objects in which there are at least two bits of non-trivial behavior. Functional test: specifies how the application behaves (from a user perspective, or close facsimile) System test: specifies how a system of applications behave These each cover progressively wider scope: unit, integration, functional, system. Based on that nomenclature, what Rails calls unit, functional, and integration tests are really integration (model + db), integration (mvc + db), and functional tests (mvc + db + routing/sessions). The rspec-rails gem aims to support the more traditional scopes by offering unit tests for controllers and views. This is what you've come up against: a controller spec is intended to be a unit test for the controller. To support this, rspec-rails (1.x) does not actually render any views at all. If the spec says the controller should render "foo/bar", and the controller renders "foo/bar", then the example passes whether or not the view exists. In a BDD process, we work from the outside-in, so before the controller spec exists, there would be a failing "integration spec" (which were called that to align with Rails' nomenclature in rspec-rails-1, but are called "request specs" in rspec-rails-2 - perhaps we should really call these "functional specs", but that might just make things more confusing). That serves two purposes: it describes the behavior from a user perspective (when I submit this form with valid data, then xyz happens), and it also provides test coverage that proves that the isolated parts specified in model, view, controller, and helper specs all play nice together. If you prefer to treat controller specs as Rails functional tests (which are mvc + db integration specs, but not functional tests since they bypass the router (sort of) and sessions (sort of)), you can tell rspec-rails-1 to render views with the "integrate_views" declaration ("render_views" in rspec-rails-2): # globally Spec::Runner::configure do |c| c.integrate_views end or # for one spec describe ThingsController do integrate_views ... end HTH, David From matthiassiegel at gmail.com Thu Aug 25 09:38:33 2011 From: matthiassiegel at gmail.com (Matthias Siegel) Date: Thu, 25 Aug 2011 23:08:33 +0930 Subject: [rspec-users] Tests that require a logged in user / session cookie In-Reply-To: References: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> Message-ID: <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> On 25/08/2011, at 11:10 AM, Justin Ko wrote: > > > On Wed, Aug 24, 2011 at 6:40 PM, Matthias Siegel wrote: > Hi, > > I'm fairly new to RSpec with Rails and I'm trying to work out how I can write request specs for resources that require a logged in user. > > I can't get this one to pass: > > > describe "GET /admin/account" do > > it "should have a 200 status code when logged in" do > post "/login", { :email => @user.email, :password => @user.password } > response.should redirect_to("/admin") > response.code.should eq("302") > get "/admin/account" > response.code.should eq("200") > end > > end > > > The login post part works fine and the session gets created correctly in the login method, but then the test fails at 'get "/admin/account"' because the session suddenly is empty. > > I have tried another approach where I set the session manually, to simulate a logged in user: > > > describe "GET /admin/account" do > > it "should have a 200 status code when logged in" do > session[:user_id] ||= @user.id > get "/admin/account" > response.code.should eq("200") > end > > end > > > But again the session arrives empty in my authorisation method when trying 'get "/admin/account"'. > > My guess is that it fails because the session relies on cookies and in test mode there obviously is no browser and no cookie. > Are there ways to simulate a logged in user in an app that creates sessions with cookies? > > Thanks for any suggestions > > What you are doing *should* work. Are there any before_filters altering the session? Maybe a gem doing it? Maybe you have an admin namespace that calls/uses a different session? I have an admin namespace, but does that effect the normal 'session' object in any way? I've done some more tests and setting the session in the RSpec code via session[:user_id] =|| @user.id definitely works, however when the GET request starts, the session is empty in the application_controller before anything else is executed. I still can't figure out where the session gets lost between RSpec and the app. I reduced the gems to a minimum set of Rails, Mongoid, BCrypt, Mail, RSpec, Cucumber and Factory_Girl, but didn't make a difference. Forgery protection is disabled for test environment. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Thu Aug 25 10:15:37 2011 From: jko170 at gmail.com (Justin Ko) Date: Thu, 25 Aug 2011 08:15:37 -0600 Subject: [rspec-users] Tests that require a logged in user / session cookie In-Reply-To: <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> References: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> Message-ID: On Thu, Aug 25, 2011 at 7:38 AM, Matthias Siegel wrote: > > On 25/08/2011, at 11:10 AM, Justin Ko wrote: > > > > On Wed, Aug 24, 2011 at 6:40 PM, Matthias Siegel > wrote: > >> Hi, >> >> I'm fairly new to RSpec with Rails and I'm trying to work out how I can >> write request specs for resources that require a logged in user. >> >> I can't get this one to pass: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> post "/login", { :email => @user.email, :password => @user.password } >> response.should redirect_to("/admin") >> response.code.should eq("302") >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> The login post part works fine and the session gets created correctly in >> the login method, but then the test fails at 'get "/admin/account"' because >> the session suddenly is empty. >> >> I have tried another approach where I set the session manually, to >> simulate a logged in user: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> session[:user_id] ||= @user.id >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> But again the session arrives empty in my authorisation method when trying >> 'get "/admin/account"'. >> >> My guess is that it fails because the session relies on cookies and in >> test mode there obviously is no browser and no cookie. >> Are there ways to simulate a logged in user in an app that creates >> sessions with cookies? >> >> Thanks for any suggestions >> > > What you are doing *should* work. Are there any before_filters altering the > session? Maybe a gem doing it? Maybe you have an admin namespace that > calls/uses a different session? > > > > I have an admin namespace, but does that effect the normal 'session' object > in any way? > > I've done some more tests and setting the session in the RSpec code via > session[:user_id] =|| @user.id definitely works, however when the GET > request starts, the session is empty in the application_controller before > anything else is executed. I still can't figure out where the session gets > lost between RSpec and the app. I reduced the gems to a minimum set of > Rails, Mongoid, BCrypt, Mail, RSpec, Cucumber and Factory_Girl, but didn't > make a difference. > > Forgery protection is disabled for test environment. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users I assume you're on the latest version of Rails and RSpec? Also, I'm going to need to see some code. The spec and the controller action (and before_filter). -------------- next part -------------- An HTML attachment was scrubbed... URL: From phillipkoebbe at gmail.com Thu Aug 25 10:41:17 2011 From: phillipkoebbe at gmail.com (Phillip Koebbe) Date: Thu, 25 Aug 2011 09:41:17 -0500 Subject: [rspec-users] doesn't rspec check the existence of template file In-Reply-To: <68DACD7E-FE22-4631-90E4-C02F42F9A370@gmail.com> References: <3ea58df2-7ccd-4e22-9f2d-df9518de838f@y4g2000vbx.googlegroups.com> <68DACD7E-FE22-4631-90E4-C02F42F9A370@gmail.com> Message-ID: <4E565F0D.3030108@gmail.com> On 2011-08-25 7:04 AM, David Chelimsky wrote: > On Aug 23, 2011, at 5:02 AM, foreverman wrote: > >> Hey, >> >> I am using rspec (1.3.0) and rspec-rails (1.3.2) for my current rails2 >> project, I found that in my controller test, rspec doesn't check the >> existence of view template. For example, >> it "should render show template" do >> get :show >> response.should render_template :show >> end >> >> The above controller test will pass even the 'show' template doesn't >> exist. >> >> I am curious if this is a bug or I did something wrong. > > Neither. There's a bit of history here so bear with me. > > The names used in Rails for the different kinds of tests it offers do not align with the names we used to use before Rails came along. Here are some older definitions (which are the ones to which I subscribe, but that doesn't mean "they are the right ones"): > > Unit test: specifies behavior of an object in isolation. > Integration test: specifies behavior of two or more objects in which there are at least two bits of non-trivial behavior. > Functional test: specifies how the application behaves (from a user perspective, or close facsimile) > System test: specifies how a system of applications behave > > These each cover progressively wider scope: unit, integration, functional, system. > > Based on that nomenclature, what Rails calls unit, functional, and integration tests are really integration (model + db), integration (mvc + db), and functional tests (mvc + db + routing/sessions). > > The rspec-rails gem aims to support the more traditional scopes by offering unit tests for controllers and views. This is what you've come up against: a controller spec is intended to be a unit test for the controller. To support this, rspec-rails (1.x) does not actually render any views at all. If the spec says the controller should render "foo/bar", and the controller renders "foo/bar", then the example passes whether or not the view exists. In a BDD process, we work from the outside-in, so before the controller spec exists, there would be a failing "integration spec" (which were called that to align with Rails' nomenclature in rspec-rails-1, but are called "request specs" in rspec-rails-2 - perhaps we should really call these "functional specs", but that might just make things more confusing). That serves two purposes: it describes the behavior from a user perspective (when I submit this form with valid data, then xyz happens), and it also provides test coverage that prove > s that the isolated parts specified in model, view, controller, and helper specs all play nice together. > > If you prefer to treat controller specs as Rails functional tests (which are mvc + db integration specs, but not functional tests since they bypass the router (sort of) and sessions (sort of)), you can tell rspec-rails-1 to render views with the "integrate_views" declaration ("render_views" in rspec-rails-2): > > # globally > Spec::Runner::configure do |c| > c.integrate_views > end > > or > > # for one spec > describe ThingsController do > integrate_views > ... > end > > HTH, > David Just throwing out my own experience with this issue. I tried the integrate_views approach, but then had to mock so much stuff so the view wouldn't generate errors it became an incredible burden. But I still wanted to know that I was rendering the right view at the right time. I came up with something that is simple, albeit a bit verbose: In the controller, I explicitly call render (meaning, I don't let Rails magic happen): def index render :index end Then in the controller test, I set an expectation: describe 'get index' do it 'should render the index template' do controller.should_receive(:render).with(:index) get :index end end Phillip From apremdas at gmail.com Thu Aug 25 12:27:44 2011 From: apremdas at gmail.com (Andrew Premdas) Date: Thu, 25 Aug 2011 17:27:44 +0100 Subject: [rspec-users] How should I interpret RCov, code coverage? In-Reply-To: References: Message-ID: On 25 August 2011 09:14, Andrei Ursan wrote: > First of all I'm new to RCov. > > I'm having a rails application with around 20 models and 20 controllers > + helpers and others. > > And I've got an unusual good RCov test coverage, 39%(total) and 31%(code > coverage) - and this with only 12 RSpec examples. > > I'm running RCov with the following options: > > t.rcov_opts = "--callsites --xrefs --no-comments --rails --exclude > test/*,spec/*,features/*,factories/*,gems/*" > > What kind of heuristic is RCov using? I'm reading its manual > http://www.linuxcertif.com/man/1/rcov/ and it says: > > > rcov is a code coverage tool for Ruby. It creates code coverage > > reports showing the unit test coverage of the target code. > > > > rcov does "statement coverage", also referred to as "C0 coverage > analysis". It > > tests, if each line of the source code has been executed. > > > > rcov is typically used to find the areas of a program that have not > > been sufficiently tested. It reports, what code has not been run by > > any test cases. > > That being said, it means that: > > (in my case) 31% Lines Of Code of the application logic have been > executed by the test files. E.g. if a method has been executed for one > test case than that method (the LOC that represent that method) are 100% > tested. The bad side is that if a test file loads a Class A (just by > including its name into its logic), and doesn't execute any of its > method, the methods signatures of that class will count as executed LOC > 100% tested, therefore the number can grow quite easy. **Right?** > > > Is an RCov code coverage of 100% really good? Because in my opinion a > method should be tested for more than one case but rcov doesn't care > about this :(. > > Is there another tool which does a better job on rails projects than > RCov on test coverage? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > You can get lots of interesting metrics for upto date rails projects using the metrical gem. Integrating such a tool into Continuous Integration is a really good idea. Viewing metrics over a time period and seeing how they change can be very informative. All metrics need to be interpreted relative to the context of the project and the metric. So the answer to your question "Is an RCov code coverage of 100% really good" is - it depends! What you really need to do here is ask better questions :) - which is not easy! The way to use metrics (IMO) is as indicators. With your current c0 coverage of only 39% you can find lots of code that is not tested. You can then find out who wrote the code, and start dealing with the issue of why untested code was added to the project. This reason will vary wildly between projects. Some example reasons will illustrate this - There is only one developer on the project and they are new to TDD. - There is only one developer on the project contributing code with no tests. He refuses to write tests for his code - All the developers want to do TDD, but as they come up to the end of each sprint they feel under to much time pressure to get things done, and so stop doing TDD ... ad infinitum The metric is an indicator there is a problem, but it does not tell you what the problem is. Using multiple metrics and viewing them over time will give you many indicators of possible problems with a project. Properly identifying a problem, with enough precision to have a chance of implementing solutions for it, requires the investigation of the source, developers and the process of development. HTH Andrew -- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From lenny at aps.org Thu Aug 25 12:40:05 2011 From: lenny at aps.org (Lenny Marks) Date: Thu, 25 Aug 2011 12:40:05 -0400 Subject: [rspec-users] rspec macro style Message-ID: <1FE3C0CF-CB5B-4C14-91D2-47F0F24F08E0@aps.org> One of the things that always annoys me when I write/use typical macros in my specs is how the backtrace from failures don't include the caller of the macro. Not a huge deal because the example name can be used to track it down, but I lose the ability to do things like click on the line from the backtrace in my IDE(I use RubyMine). I find that feature to be a nice time saver. I've never heard any mention of this bothering anybody so maybe I'm just being pedantic but tweaking things so that the actual example implementation is yielded by the caller seems like a better style to me because it solves that problem. Just curious if anybody else has any thoughts one way or the other. I've been leaning toward the latter when possible. Full example at: https://gist.github.com/1169172 ex. When the example below fails, line 17 is significant 5 describe Email do 6 describe '.normalize', ' should strip email down to core address' do 7 def self.normalized(original, options) 8 it %{normalizes '#{original}' as '#{options[:should_be]}'} do 9 Email.normalize(original).should == options[:should_be] 10 end 11 end ..... 16 describe 'it strips whitespace' do 17 normalized(' joe at somewhere.com', :should_be => 'joe at somewhere.com') 18 end Failures: 1) Email.normalize should strip email down to core address it strips whitespace normalizes ' joe at somewhere.com' as 'joe at somewhere.com' Failure/Error: Email.normalize(original).should == options[:should_be] expected: "joe at somewhere.com" got: " joe at somewhere.com" (using ==) # org/jruby/RubyProc.java:268:in `call' # ./spec/lib/email_spec.rb:9:in `normalized' # org/jruby/RubyKernel.java:2028:in `instance_eval' Compare to this where the significant lines are present in the backtrace describe Email do 6 describe '.normalize', ' should strip email down to core address' do 7 def self.normalized(original, &blk) 8 describe "'#{original}'" do 9 subject { Email.normalize(original) } 10 it { instance_eval(&blk) } 11 end 12 end ...... 17 describe 'it strips whitespace' do 18 normalized(' joe at somewhere.com') { should == 'joe at somewhere.com' } 19 end Failures: 1) Email.normalize should strip email down to core address it strips whitespace ' joe at somewhere.com' Failure/Error: normalized(' joe at somewhere.com') { should == 'joe at somewhere.com' } expected: "joe at somewhere.com" got: " joe at somewhere.com" (using ==) # org/jruby/RubyProc.java:268:in `call' # ./spec/lib/email_spec.rb:18:in `(root)' # org/jruby/RubyKernel.java:2028:in `instance_eval' # ./spec/lib/email_spec.rb:10:in `normalized' # org/jruby/RubyKernel.java:2028:in `instance_eval' ... -lenny From apremdas at gmail.com Thu Aug 25 12:42:49 2011 From: apremdas at gmail.com (Andrew Premdas) Date: Thu, 25 Aug 2011 17:42:49 +0100 Subject: [rspec-users] Tests that require a logged in user / session cookie In-Reply-To: <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> References: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> Message-ID: On 25 August 2011 14:38, Matthias Siegel wrote: > > On 25/08/2011, at 11:10 AM, Justin Ko wrote: > > > > On Wed, Aug 24, 2011 at 6:40 PM, Matthias Siegel > wrote: > >> Hi, >> >> I'm fairly new to RSpec with Rails and I'm trying to work out how I can >> write request specs for resources that require a logged in user. >> >> I can't get this one to pass: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> post "/login", { :email => @user.email, :password => @user.password } >> response.should redirect_to("/admin") >> response.code.should eq("302") >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> The login post part works fine and the session gets created correctly in >> the login method, but then the test fails at 'get "/admin/account"' because >> the session suddenly is empty. >> >> I have tried another approach where I set the session manually, to >> simulate a logged in user: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> session[:user_id] ||= @user.id >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> But again the session arrives empty in my authorisation method when trying >> 'get "/admin/account"'. >> >> My guess is that it fails because the session relies on cookies and in >> test mode there obviously is no browser and no cookie. >> Are there ways to simulate a logged in user in an app that creates >> sessions with cookies? >> >> Thanks for any suggestions >> > > What you are doing *should* work. Are there any before_filters altering the > session? Maybe a gem doing it? Maybe you have an admin namespace that > calls/uses a different session? > > > > I have an admin namespace, but does that effect the normal 'session' object > in any way? > > I've done some more tests and setting the session in the RSpec code via > session[:user_id] =|| @user.id definitely works, however when the GET > request starts, the session is empty in the application_controller before > anything else is executed. I still can't figure out where the session gets > lost between RSpec and the app. I reduced the gems to a minimum set of > Rails, Mongoid, BCrypt, Mail, RSpec, Cucumber and Factory_Girl, but didn't > make a difference. > > Forgery protection is disabled for test environment. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > If you are using Cucumber (which is in your minimal set of Gems), you don't need to write this sort of test. A Cucumber feature will already cover this. RSpec has a wealth of testing tools to cover all sorts of different environments/conditions/styles. This makes it very easy for you to waste time writing tests you don't need. For Rails applications with cucumber and rspec, you only really need to write model specs and features (if you keep your controllers, skinny) *. If a test is difficult to do its always worth thinking, Why am I doing this test? Could I do another test thats easier? HTH Andrew * I don't expect everyone to agree with this -- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Fri Aug 26 00:55:00 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 25 Aug 2011 23:55:00 -0500 Subject: [rspec-users] rspec macro style In-Reply-To: <1FE3C0CF-CB5B-4C14-91D2-47F0F24F08E0@aps.org> References: <1FE3C0CF-CB5B-4C14-91D2-47F0F24F08E0@aps.org> Message-ID: On Aug 25, 2011, at 11:40 AM, Lenny Marks wrote: > One of the things that always annoys me when I write/use typical macros in my specs is how the backtrace from failures don't include the caller of the macro. Not a huge deal because the example name can be used to track it down, but I lose the ability to do things like click on the line from the backtrace in my IDE(I use RubyMine). I find that feature to be a nice time saver. I've never heard any mention of this bothering anybody so maybe I'm just being pedantic but tweaking things so that the actual example implementation is yielded by the caller seems like a better style to me because it solves that problem. Just curious if anybody else has any thoughts one way or the other. I've been leaning toward the latter when possible. > > Full example at: > https://gist.github.com/1169172 > > ex. When the example below fails, line 17 is significant > > 5 describe Email do > 6 describe '.normalize', ' should strip email down to core address' do > 7 def self.normalized(original, options) > 8 it %{normalizes '#{original}' as '#{options[:should_be]}'} do > 9 Email.normalize(original).should == options[:should_be] > 10 end > 11 end > ..... > 16 describe 'it strips whitespace' do > 17 normalized(' joe at somewhere.com', :should_be => 'joe at somewhere.com') > 18 end > > Failures: > > 1) Email.normalize should strip email down to core address it strips whitespace normalizes ' joe at somewhere.com' as 'joe at somewhere.com' > Failure/Error: Email.normalize(original).should == options[:should_be] > expected: "joe at somewhere.com" > got: " joe at somewhere.com" (using ==) > # org/jruby/RubyProc.java:268:in `call' > # ./spec/lib/email_spec.rb:9:in `normalized' > # org/jruby/RubyKernel.java:2028:in `instance_eval' If you run this with the --backtrace flag, you should see line 17 as well. > Compare to this where the significant lines are present in the backtrace > > describe Email do > 6 describe '.normalize', ' should strip email down to core address' do > 7 def self.normalized(original, &blk) > 8 describe "'#{original}'" do > 9 subject { Email.normalize(original) } > 10 it { instance_eval(&blk) } > 11 end > 12 end > ...... > 17 describe 'it strips whitespace' do > 18 normalized(' joe at somewhere.com') { should == 'joe at somewhere.com' } > 19 end > > Failures: > > 1) Email.normalize should strip email down to core address it strips whitespace ' joe at somewhere.com' > Failure/Error: normalized(' joe at somewhere.com') { should == 'joe at somewhere.com' } > expected: "joe at somewhere.com" > got: " joe at somewhere.com" (using ==) > # org/jruby/RubyProc.java:268:in `call' > # ./spec/lib/email_spec.rb:18:in `(root)' > # org/jruby/RubyKernel.java:2028:in `instance_eval' > # ./spec/lib/email_spec.rb:10:in `normalized' > # org/jruby/RubyKernel.java:2028:in `instance_eval' Interesting that that happens. I'm not clear on why. Regardless, this macro appraoch creates a lot of distance between the reader and the code that is being specified. Personally, I find this much easier to read: describe Email do describe "#normalize" do it "strips whitespace" do Email.normalize(' joe at somewhere.com').should eq('joe at somewhere.com') end end end ... and it would give exactly the spec output and failure message that I want. Don't get me wrong - I'm all for sharing code where it provides a benefit that outweighs the loss of proximity, but I would typically do that with custom matchers, shared examples or simple helper methods (at the example level rather than the group level). FWIW, David From matthiassiegel at gmail.com Fri Aug 26 02:12:33 2011 From: matthiassiegel at gmail.com (Matthias Siegel) Date: Fri, 26 Aug 2011 15:42:33 +0930 Subject: [rspec-users] Tests that require a logged in user / session cookie In-Reply-To: References: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> Message-ID: On 25/08/2011, at 11:45 PM, Justin Ko wrote: > > > On Thu, Aug 25, 2011 at 7:38 AM, Matthias Siegel wrote: > > On 25/08/2011, at 11:10 AM, Justin Ko wrote: > >> >> >> On Wed, Aug 24, 2011 at 6:40 PM, Matthias Siegel wrote: >> Hi, >> >> I'm fairly new to RSpec with Rails and I'm trying to work out how I can write request specs for resources that require a logged in user. >> >> I can't get this one to pass: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> post "/login", { :email => @user.email, :password => @user.password } >> response.should redirect_to("/admin") >> response.code.should eq("302") >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> The login post part works fine and the session gets created correctly in the login method, but then the test fails at 'get "/admin/account"' because the session suddenly is empty. >> >> I have tried another approach where I set the session manually, to simulate a logged in user: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> session[:user_id] ||= @user.id >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> But again the session arrives empty in my authorisation method when trying 'get "/admin/account"'. >> >> My guess is that it fails because the session relies on cookies and in test mode there obviously is no browser and no cookie. >> Are there ways to simulate a logged in user in an app that creates sessions with cookies? >> >> Thanks for any suggestions >> >> What you are doing *should* work. Are there any before_filters altering the session? Maybe a gem doing it? Maybe you have an admin namespace that calls/uses a different session? > > > I have an admin namespace, but does that effect the normal 'session' object in any way? > > I've done some more tests and setting the session in the RSpec code via session[:user_id] =|| @user.id definitely works, however when the GET request starts, the session is empty in the application_controller before anything else is executed. I still can't figure out where the session gets lost between RSpec and the app. I reduced the gems to a minimum set of Rails, Mongoid, BCrypt, Mail, RSpec, Cucumber and Factory_Girl, but didn't make a difference. > > Forgery protection is disabled for test environment. > > > > I assume you're on the latest version of Rails and RSpec? > > Also, I'm going to need to see some code. The spec and the controller action (and before_filter). Yes, I'm running the latest versions of all gems. I just found this: http://stackoverflow.com/questions/5235084/testing-sessions-in-rails-3-with-rspec-capybara It looks a lot like the issue that I have. The answer mentions that sessions aren't available in request specs because of capybara. To be honest I'm not much familiar with what capybara actually does but I have reproduced this by basically moving the test into a controller spec, and suddenly the session goes through and doesn't show up empty in the authorize method of my app. -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Fri Aug 26 07:55:17 2011 From: lists at ruby-forum.com (Andrei Ursan) Date: Fri, 26 Aug 2011 13:55:17 +0200 Subject: [rspec-users] How should I interpret RCov, code coverage? In-Reply-To: References: Message-ID: <31c12e698c14360f2a3181f800be29a6@ruby-forum.com> Thanks for the info and suggestions Sidu & Andrew! I've got the rails project to do some bug fixing and for adding new features. And I wanted to see some metrics before touching the code... anyway I will dig more into it - as I am new to rails and ruby but I like TDD and BDD :D. Any opinions on this topic are welcome! Thanks, Andrei. -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Fri Aug 26 09:09:20 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 26 Aug 2011 08:09:20 -0500 Subject: [rspec-users] slightly confusing error message In-Reply-To: References: <8f0ef6b409bc7413d021896c0b2ca43c@ruby-forum.com> <7382813B-5CCD-4B34-B2A6-82FC99334C78@gmail.com> <74181EFC-2D9C-4877-8285-DB0F4076EC44@AgileConsultingLLC.com> Message-ID: <5AAF5991-F3B6-4DCE-ABC6-6BD247040900@gmail.com> On Aug 24, 2011, at 11:48 AM, Rob Biedenharn wrote: > > On Aug 23, 2011, at 5:18 PM, David Chelimsky wrote: > >> >> On Aug 23, 2011, at 3:26 PM, Rob Biedenharn wrote: >> >>> On Aug 23, 2011, at 3:16 PM, David Chelimsky wrote: >>> >>>> On Aug 23, 2011, at 1:00 PM, Roger Pack wrote: >>>> >>>>> This error message: >>>>> >>>>> >>>>> it 'should' do >>>>> proc { eval("a=")}.should raise_exception(/SyntaxError/) >>>>> end >>> >>> shouldn't that be >>> raise_exception(SyntaxError) >>> that is, the exception class, not a Regexp literal to match its name. >> >> It supports any of: >> >> raise_error(ErrorClass) >> raise_error(ErrorClass, string) >> raise_error(ErrorClass, /regex/) >> raise_error(string) >> raise_error(/regex/) >> >> (or raise_exception with the same set of alternatives) >> >> Cheers, >> David > > RSpec 1.3 (from apidock) > > raise_exception(exception=Exception, message=nil, &block) public > With no args, matches if any exception is raised. With a named exception, matches only if that specific exception is raised. With a named exception and messsage specified as a String, matches only if bothmatch. With a named exception and messsage specified as a Regexp, matches only if both match. Passan optional block to perform extra verifications on the exception matched > > > Rspec 2 (from http://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/raise-error-matcher) > > only shows forms with either nothing specified, or the first argument is always the Exception class. > > If you use one of: > raise_error(string) > raise_error(/regex/) > against what is the string or /regex/ matched? The message associated to the Exception or the NAME of the exception. > >>>>> expected Exception with message matching /SyntaxError/, got >>>>> # >>>>> >>>>> Is a bit confusing, since it appears from the error output that it >>>>> *does* match /SyntaxError/ > > The MESSAGE does not match /SyntaxError/ but it would have matched /syntax error/. > > I think that's where the OP is confused. > > -Rob I beefed up the doc: https://github.com/rspec/rspec-expectations/blob/a541a7523ef282b9bf96141f5b8a0920dda55281/features/built_in_matchers/expect_error.feature#L18-23. Relish requires pushing all the docs at once and I have no way of knowing what the last commit was that I pushed (so I can't really push the current docs or roll back to the last release and patch them). So this won't make it to the relish site today, but will as soon as I can get a release out. Cheers, David >>>>> Might be able to word it better or along those lines. >>>>> Thanks! >>>>> -roger- >>>> >>>> Please report this to http://github.com/rspec/rspec-expectations/issues. >>>> >>>> Cheers, >>>> David From jko170 at gmail.com Fri Aug 26 09:46:45 2011 From: jko170 at gmail.com (Justin Ko) Date: Fri, 26 Aug 2011 07:46:45 -0600 Subject: [rspec-users] Tests that require a logged in user / session cookie In-Reply-To: References: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> Message-ID: On Fri, Aug 26, 2011 at 12:12 AM, Matthias Siegel wrote: > > On 25/08/2011, at 11:45 PM, Justin Ko wrote: > > > > On Thu, Aug 25, 2011 at 7:38 AM, Matthias Siegel > wrote: > >> >> On 25/08/2011, at 11:10 AM, Justin Ko wrote: >> >> >> >> On Wed, Aug 24, 2011 at 6:40 PM, Matthias Siegel < >> matthiassiegel at gmail.com> wrote: >> >>> Hi, >>> >>> I'm fairly new to RSpec with Rails and I'm trying to work out how I can >>> write request specs for resources that require a logged in user. >>> >>> I can't get this one to pass: >>> >>> >>> describe "GET /admin/account" do >>> >>> it "should have a 200 status code when logged in" do >>> post "/login", { :email => @user.email, :password => @user.password } >>> response.should redirect_to("/admin") >>> response.code.should eq("302") >>> get "/admin/account" >>> response.code.should eq("200") >>> end >>> >>> end >>> >>> >>> The login post part works fine and the session gets created correctly in >>> the login method, but then the test fails at 'get "/admin/account"' because >>> the session suddenly is empty. >>> >>> I have tried another approach where I set the session manually, to >>> simulate a logged in user: >>> >>> >>> describe "GET /admin/account" do >>> >>> it "should have a 200 status code when logged in" do >>> session[:user_id] ||= @user.id >>> get "/admin/account" >>> response.code.should eq("200") >>> end >>> >>> end >>> >>> >>> But again the session arrives empty in my authorisation method when >>> trying 'get "/admin/account"'. >>> >>> My guess is that it fails because the session relies on cookies and in >>> test mode there obviously is no browser and no cookie. >>> Are there ways to simulate a logged in user in an app that creates >>> sessions with cookies? >>> >>> Thanks for any suggestions >>> >> >> What you are doing *should* work. Are there any before_filters altering >> the session? Maybe a gem doing it? Maybe you have an admin namespace that >> calls/uses a different session? >> >> >> >> I have an admin namespace, but does that effect the normal 'session' >> object in any way? >> >> I've done some more tests and setting the session in the RSpec code via >> session[:user_id] =|| @user.id definitely works, however when the GET >> request starts, the session is empty in the application_controller before >> anything else is executed. I still can't figure out where the session gets >> lost between RSpec and the app. I reduced the gems to a minimum set of >> Rails, Mongoid, BCrypt, Mail, RSpec, Cucumber and Factory_Girl, but didn't >> make a difference. >> >> Forgery protection is disabled for test environment. >> >> >> > I assume you're on the latest version of Rails and RSpec? > > Also, I'm going to need to see some code. The spec and the controller > action (and before_filter). > > > > Yes, I'm running the latest versions of all gems. > > I just found this: > > http://stackoverflow.com/questions/5235084/testing-sessions-in-rails-3-with-rspec-capybara > > It looks a lot like the issue that I have. > > The answer mentions that sessions aren't available in request specs because > of capybara. To be honest I'm not much familiar with what capybara actually > does but I have reproduced this by basically moving the test into a > controller spec, and suddenly the session goes through and doesn't show up > empty in the authorize method of my app. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > Oh jeez, I thought you were using a controller spec the entire time because of the methods being used. Yes, capybara restricts you from accessing the session, which is a *good thing* for request specs. -------------- next part -------------- An HTML attachment was scrubbed... URL: From phillipkoebbe at gmail.com Fri Aug 26 10:40:28 2011 From: phillipkoebbe at gmail.com (Phillip Koebbe) Date: Fri, 26 Aug 2011 09:40:28 -0500 Subject: [rspec-users] Tests that require a logged in user / session cookie In-Reply-To: References: <2237C018-D1B5-448F-972E-8E156260905E@gmail.com> <320170A7-69CA-48F4-9735-0D5B27532E62@gmail.com> Message-ID: <4E57B05C.7080706@gmail.com> On 2011-08-25 11:42 AM, Andrew Premdas wrote: > On 25 August 2011 14:38, Matthias Siegel > wrote: > > > On 25/08/2011, at 11:10 AM, Justin Ko wrote: > >> >> >> On Wed, Aug 24, 2011 at 6:40 PM, Matthias Siegel >> > wrote: >> >> Hi, >> >> I'm fairly new to RSpec with Rails and I'm trying to work out >> how I can write request specs for resources that require a >> logged in user. >> >> I can't get this one to pass: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> post "/login", { :email => @user.email, :password => >> @user.password } >> response.should redirect_to("/admin") >> response.code.should eq("302") >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> The login post part works fine and the session gets created >> correctly in the login method, but then the test fails at >> 'get "/admin/account"' because the session suddenly is empty. >> >> I have tried another approach where I set the session >> manually, to simulate a logged in user: >> >> >> describe "GET /admin/account" do >> >> it "should have a 200 status code when logged in" do >> session[:user_id] ||= @user.id >> get "/admin/account" >> response.code.should eq("200") >> end >> >> end >> >> >> But again the session arrives empty in my authorisation >> method when trying 'get "/admin/account"'. >> >> My guess is that it fails because the session relies on >> cookies and in test mode there obviously is no browser and no >> cookie. >> Are there ways to simulate a logged in user in an app that >> creates sessions with cookies? >> >> Thanks for any suggestions >> >> >> What you are doing *should* work. Are there any before_filters >> altering the session? Maybe a gem doing it? Maybe you have an >> admin namespace that calls/uses a different session? > > > I have an admin namespace, but does that effect the normal > 'session' object in any way? > > I've done some more tests and setting the session in the RSpec > code via session[:user_id] =|| @user.id > definitely works, however when the GET request starts, the session > is empty in the application_controller before anything else is > executed. I still can't figure out where the session gets lost > between RSpec and the app. I reduced the gems to a minimum set of > Rails, Mongoid, BCrypt, Mail, RSpec, Cucumber and Factory_Girl, > but didn't make a difference. > > Forgery protection is disabled for test environment. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > If you are using Cucumber (which is in your minimal set of Gems), you > don't need to write this sort of test. A Cucumber feature will already > cover this. RSpec has a wealth of testing tools to cover all sorts of > different environments/conditions/styles. This makes it very easy for > you to waste time writing tests you don't need. For Rails applications > with cucumber and rspec, you only really need to write model specs and > features (if you keep your controllers, skinny) *. If a test is > difficult to do its always worth thinking, Why am I doing this test? > Could I do another test thats easier? > > HTH > > Andrew > > * I don't expect everyone to agree with this I'm of the same opinion on this issue as you, Andrew. I've been trying for a long time to keep my controllers skinny, and I've recently been thinking that the Cucumber suite should be sufficient for covering them. Like you, though, I expect there are quite a few who disagree. But, hey, so goes life... Peace. From lists at ruby-forum.com Fri Aug 26 13:02:03 2011 From: lists at ruby-forum.com (Roger Pack) Date: Fri, 26 Aug 2011 19:02:03 +0200 Subject: [rspec-users] warn on stubbing nonexistent method? Message-ID: <2c0844efffe4720694d5c5a21cdad483@ruby-forum.com> Hello. Perhaps rspec-mocks could warn if it stubs a not yet existing method? class A end describe RSpec do it 'should' do a = A.new a.stub!(:nonexistent_method) {} end end This might help the development process by pointing out possible discrepancies between rspec tests and the code (for when refactoring wasn't performed fully et al). Cheers! -roger- -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Fri Aug 26 13:48:32 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 26 Aug 2011 12:48:32 -0500 Subject: [rspec-users] warn on stubbing nonexistent method? In-Reply-To: <2c0844efffe4720694d5c5a21cdad483@ruby-forum.com> References: <2c0844efffe4720694d5c5a21cdad483@ruby-forum.com> Message-ID: <9B259E7E-1FF9-456E-AC9A-3D93869C7F2F@gmail.com> On Aug 26, 2011, at 12:02 PM, Roger Pack wrote: > Hello. > Perhaps rspec-mocks could warn if it stubs a not yet existing method? See https://github.com/rspec/rspec-mocks/issues/15 Cheers, David From dolzenko at gmail.com Fri Aug 26 14:28:46 2011 From: dolzenko at gmail.com (Evgeniy Dolzhenko) Date: Fri, 26 Aug 2011 22:28:46 +0400 Subject: [rspec-users] warn on stubbing nonexistent method? In-Reply-To: <2c0844efffe4720694d5c5a21cdad483@ruby-forum.com> References: <2c0844efffe4720694d5c5a21cdad483@ruby-forum.com> Message-ID: <4E57E5DE.6030808@gmail.com> Roger, Check the list archives - this topic is brought up every other 3 months :) On 8/26/2011 9:02 PM, Roger Pack wrote: > Hello. > Perhaps rspec-mocks could warn if it stubs a not yet existing method? > > > class A > end > > describe RSpec do > it 'should' do > a = A.new > a.stub!(:nonexistent_method) {} > end > end > > This might help the development process by pointing out possible > discrepancies between rspec tests and the code (for when refactoring > wasn't performed fully et al). > Cheers! > -roger- > From mikerin.slava at gmail.com Sun Aug 28 03:03:23 2011 From: mikerin.slava at gmail.com (slavix) Date: Sun, 28 Aug 2011 00:03:23 -0700 (PDT) Subject: [rspec-users] troubles running rspec.. Message-ID: <283850ef-4a15-4c22-84e8-6915d965de9b@x11g2000prb.googlegroups.com> developing a 3.1 app. trying to run spec on and get this error: bundle exec rake spec ***************************************************************** DEPRECATION WARNING: you are using a deprecated constant that will be removed from a future version of RSpec. /home/slava/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/ runtime.rb:68:in `require' * Rspec is deprecated. * RSpec is the new top-level module in RSpec-2 *************************************************************** [DEPRECATION WARNING] Nested I18n namespace lookup under "activerecord.attributes.order" is no longer supported /home/slava/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -S bundle exec rspec ./spec/models/derivative_spec.rb /home/slava/.rvm/gems/ruby-1.9.2-p290/gems/capybara-1.0.1/lib/capybara/ rails.rb:15:in `': undefined method `join' for nil:NilClass (NoMethodError) from /home/slava/.rvm/gems/ruby-1.9.2-p290/gems/ activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:237:in `require' From mikerin.slava at gmail.com Sun Aug 28 06:12:35 2011 From: mikerin.slava at gmail.com (slavix) Date: Sun, 28 Aug 2011 03:12:35 -0700 (PDT) Subject: [rspec-users] troubles running rspec.. In-Reply-To: <283850ef-4a15-4c22-84e8-6915d965de9b@x11g2000prb.googlegroups.com> References: <283850ef-4a15-4c22-84e8-6915d965de9b@x11g2000prb.googlegroups.com> Message-ID: <9fb29455-d22f-49b8-9db5-6fc0143f6e85@c8g2000prn.googlegroups.com> never mind. I didn't run rails generate rspec:install On Aug 28, 12:03?am, slavix wrote: > developing a 3.1 app. trying to run spec on and get this error: > > ?bundle exec rake spec > ***************************************************************** > DEPRECATION WARNING: you are using a deprecated constant that will > be removed from a future version of RSpec. > > /home/slava/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/ > runtime.rb:68:in `require' > > * Rspec is deprecated. > * RSpec is the new top-level module in RSpec-2 > *************************************************************** > > [DEPRECATION WARNING] Nested I18n namespace lookup under > "activerecord.attributes.order" is no longer supported > /home/slava/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -S bundle exec > rspec ./spec/models/derivative_spec.rb > /home/slava/.rvm/gems/ruby-1.9.2-p290/gems/capybara-1.0.1/lib/capybara/ > rails.rb:15:in `': undefined method `join' for > nil:NilClass (NoMethodError) > ? ? ? ? from /home/slava/.rvm/gems/ruby-1.9.2-p290/gems/ > activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:237:in > `require' > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From alexey.v.zaharov at gmail.com Thu Aug 25 12:20:26 2011 From: alexey.v.zaharov at gmail.com (Alexey Zakharov) Date: Thu, 25 Aug 2011 09:20:26 -0700 (PDT) Subject: [rspec-users] What is the right method to call Rspec.configure when spork is used? prefork or each_run? Message-ID: <16293420.800.1314289226381.JavaMail.geo-discussion-forums@yqic37> I need to setup mongoid collections clean up for each spec. What is the right method to call Rspec.configure when spork is used? prefork or each_run? Here is my current setup: require 'rubygems' require 'spork' Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. require File.dirname(__FILE__) + '/../config/environment.rb' require 'rspec' require 'rspec/rails' RSpec.configure do |config| config.mock_with :rspec config.after(:each) do puts "cleaning mongodb...." Mongoid.database.collections.each do |collection| unless collection.name =~ /^system\./ collection.remove end end puts "finished cleaning mongodb." end end end Spork.each_run do end -------------- next part -------------- An HTML attachment was scrubbed... URL: From nick.rutherford at gmail.com Sun Aug 28 09:57:38 2011 From: nick.rutherford at gmail.com (nruth) Date: Sun, 28 Aug 2011 06:57:38 -0700 (PDT) Subject: [rspec-users] rspec-rails render_views doesn't render layouts? / how to check flash isn't rendered Message-ID: <15216093.2997.1314539858924.JavaMail.geo-discussion-forums@yqlo5> I'm trying to test that "static" pages (they're ERB, but get cached), generated through rails, don't render any stray flash notices left over by the authentication system (Devise) or wherever else. I've tried writing this controller spec, but it appears that response.body only renders the template, not its layouts? describe "so that static caching can be used" do render_views specify "flash notices are not rendered" do flash[:notice] = "flash boo" flash[:error] = "flash boo" flash[:alert] = "flash boo" get :show, :page => 'privacy_policy' response.body.should have_content('flash boo') end end class StaticPagesController < ApplicationController layout 'master' def show response.headers['Cache-Control'] = "public, max-age=#{6.hours}" render "static_pages/#{params[:page]}" end end I've tried changing to a layout which does render flash notices, and even inserting the text into the layout template, but can't make the spec fail. Is there a way to ask rspec to render the template with the appropriate layouts as well? Is a controller spec the wrong way to try and do this? It seems out of place, as it's more to do with which layouts are being used, and their contents, but the rendering process starts at the controller, it receives the result, and I can manipulate the flash hash contents before rendering. Versions: rails (3.0.10) rspec-rails (2.6.1) rspec-core (2.6.4) I've also posted on stackoverflow @ http://stackoverflow.com/questions/7221388/rspec-render-views-ignores-layouts-want-to-test-static-cached-page-does-not-disp Thanks, Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: From chabgood at gmail.com Sun Aug 28 14:40:53 2011 From: chabgood at gmail.com (Chris Habgood) Date: Sun, 28 Aug 2011 11:40:53 -0700 (PDT) Subject: [rspec-users] mongo_mapper Message-ID: <16615321.184.1314556853874.JavaMail.geo-discussion-forums@yqic37> I have a rails 3.1 app. I am using mongo_mapper, but when I run rake rspec it says: Mongoid is not installed (gem install mongoid) Any idea where this could be coming from. I am using: gem 'machinist', '>= 2.0.0.beta2' gem 'machinist2_mongomapper', :require => "machinist_mongomapper" gem 'machinist_mongo', :require => 'machinist/mongo_mapper', :git => 'https://github.com/nmerouze/machinist_mongo.git', :branch => 'machinist2' -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Sun Aug 28 23:27:33 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 28 Aug 2011 22:27:33 -0500 Subject: [rspec-users] What is the right method to call Rspec.configure when spork is used? prefork or each_run? In-Reply-To: <16293420.800.1314289226381.JavaMail.geo-discussion-forums@yqic37> References: <16293420.800.1314289226381.JavaMail.geo-discussion-forums@yqic37> Message-ID: On Aug 25, 2011, at 11:20 AM, Alexey Zakharov wrote: > I need to setup mongoid collections clean up for each spec. What is the right method to call Rspec.configure when spork is used? prefork or each_run? > Either should be fine for a before or after hook, as long as they depend on any global data (which your example does not). HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Sun Aug 28 23:31:42 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 28 Aug 2011 22:31:42 -0500 Subject: [rspec-users] mongo_mapper In-Reply-To: <16615321.184.1314556853874.JavaMail.geo-discussion-forums@yqic37> References: <16615321.184.1314556853874.JavaMail.geo-discussion-forums@yqic37> Message-ID: On Aug 28, 2011, at 1:40 PM, Chris Habgood wrote: > I have a rails 3.1 app. I am using mongo_mapper, but when I run rake rspec it says: > > Mongoid is not installed (gem install mongoid) > > Any idea where this could be coming from. > > I am using: > > gem 'machinist', '>= 2.0.0.beta2' > gem 'machinist2_mongomapper', :require => "machinist_mongomapper" > gem 'machinist_mongo', :require => 'machinist/mongo_mapper', :git => 'https://github.com/nmerouze/machinist_mongo.git', :branch => 'machinist2' Is this what's in your Gemfile? What do you get when you type `bundle check`? From dchelimsky at gmail.com Mon Aug 29 00:23:08 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 28 Aug 2011 23:23:08 -0500 Subject: [rspec-users] rspec-rails render_views doesn't render layouts? / how to check flash isn't rendered In-Reply-To: <15216093.2997.1314539858924.JavaMail.geo-discussion-forums@yqlo5> References: <15216093.2997.1314539858924.JavaMail.geo-discussion-forums@yqlo5> Message-ID: On Aug 28, 2011, at 8:57 AM, nruth wrote: > I'm trying to test that "static" pages (they're ERB, but get cached), generated through rails, don't render any stray flash notices left over by the authentication system (Devise) or wherever else. > > I've tried writing this controller spec, but it appears that response.body only renders the template, not its layouts? If you use render_views, as in the example below, layouts should be rendered. This is managed by Rails, not RSpec (without render_views, rspec-rails stubs the view, but with render_views you're getting exactly what you'd get in a Rails functional test). > describe "so that static caching can be used" do > render_views > specify "flash notices are not rendered" do > flash[:notice] = "flash boo" > flash[:error] = "flash boo" > flash[:alert] = "flash boo" These lines ^^ have no effect. The flash object before the get (below) is not the same as the flash object used in the controller. > get :show, :page => 'privacy_policy' > response.body.should have_content('flash boo') > end > end > > class StaticPagesController < ApplicationController > layout 'master' > > def show > response.headers['Cache-Control'] = "public, max-age=#{6.hours}" > render "static_pages/#{params[:page]}" > end > end I guess I'm confused about your goal here: are you trying to specify that the page, when it gets cached, doesn't include any flash messages? If that's the case, then you'd probably want to do this in an integration test where you hit the url that causes the page to be cached, followed by a request for the cached page itself. HTH, David From nick.rutherford at gmail.com Mon Aug 29 07:38:52 2011 From: nick.rutherford at gmail.com (nruth) Date: Mon, 29 Aug 2011 04:38:52 -0700 (PDT) Subject: [rspec-users] rspec-rails render_views doesn't render layouts? / how to check flash isn't rendered In-Reply-To: References: <15216093.2997.1314539858924.JavaMail.geo-discussion-forums@yqlo5> Message-ID: <1083856.5362.1314617932415.JavaMail.geo-discussion-forums@yqej16> Hi David Something like: Given I am a user about to receive an arbitrary flash notice When I visit one of the application's static pages which has not yet been cached Then I should not see my flash notice in the page And another user should not see my flash notice when they visit the same page afterwards The aim is to produce a sort of tripwire test which checks that nobody has added <%= flash[:notice] %> or similar to any of the ERB files involved in rendering this page. It's to prevent a problem where someone might log out (devise), be sent to / and trigger static (or varnish) page caching saying "You have been logged out" which everyone else would then also see. I agree it's more of an integration test, as it's testing the view and controller, so I'll try a request spec with dummy controller/routing to set a flash message next. I'd thought I could manipulate the flash in a controller spec, but now I remember it's for inspecting it after a request. Thanks for the help Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: From nick.rutherford at gmail.com Mon Aug 29 09:35:32 2011 From: nick.rutherford at gmail.com (nruth) Date: Mon, 29 Aug 2011 06:35:32 -0700 (PDT) Subject: [rspec-users] rspec-rails render_views doesn't render layouts? / how to check flash isn't rendered In-Reply-To: <1083856.5362.1314617932415.JavaMail.geo-discussion-forums@yqej16> References: <15216093.2997.1314539858924.JavaMail.geo-discussion-forums@yqlo5> <1083856.5362.1314617932415.JavaMail.geo-discussion-forums@yqej16> Message-ID: <11946687.214.1314624932688.JavaMail.geo-discussion-forums@yqic37> I've shared my solution here in case it's of interest: https://gist.github.com/1178383 I don't like the use of reload_routes! but it seems to do the trick. Regards, Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: From ash.moran at patchspace.co.uk Mon Aug 29 12:24:10 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Mon, 29 Aug 2011 17:24:10 +0100 Subject: [rspec-users] Selectively ignoring exceptions in examples Message-ID: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> Hi all Long time since I've posted to rspec-users. Glad to see the place is still here and hope you're all well :-) I have a question about ignoring exceptions when they're not interesting. For example, I have a few cases in my code along these lines? it "prints an error" do expect { run_command(%w[ missing_wallet.dat ]) }.to raise_error stream_bundle.captured_error.should eq "Couldn't find wallet file: missing_wallet.dat\n" end it "raises a CLI::CommandError" do expect { run_command(%w[ missing_wallet.dat ]) }.to raise_error(CLI::CommandError) end But in the first example, I'm only bothered about the output, not the error. So I was thinking of writing something along the lines of: it "prints an error" do ignoring_errors { run_command(%w[ missing_wallet.dat ]) } stream_bundle.captured_error.should eq "Couldn't find wallet file: missing_wallet.dat\n" end Now obviously that wouldn't be hard to add as a helper method. But it got me thinking? * Do any of you do this? * Does RSpec already let you somehow? * Is it a useful convention? * Is it hiding anything else? (I don't use exceptions much, so I may be abusing them.) Cheers Ash -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From nick at deadorange.com Mon Aug 29 15:09:39 2011 From: nick at deadorange.com (Nick) Date: Mon, 29 Aug 2011 12:09:39 -0700 (PDT) Subject: [rspec-users] Selectively ignoring exceptions in examples In-Reply-To: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> References: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> Message-ID: <1004151.382.1314644979207.JavaMail.geo-discussion-forums@vbsv31> Hey there, Ash. Why not put your call to #run_command inside a begin-rescue statement? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ash.moran at patchspace.co.uk Mon Aug 29 17:08:00 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Mon, 29 Aug 2011 22:08:00 +0100 Subject: [rspec-users] Selectively ignoring exceptions in examples In-Reply-To: <1004151.382.1314644979207.JavaMail.geo-discussion-forums@vbsv31> References: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> <1004151.382.1314644979207.JavaMail.geo-discussion-forums@vbsv31> Message-ID: On 29 Aug 2011, at 20:09, Nick wrote: > Hey there, Ash. Why not put your call to #run_command inside a begin-rescue statement? Hi Nick Yes I'm missing the obvious as usual* :-) Well I guess I could, but the syntax then is even more intrusive. I guess I just want the lightest way possible to say "Yes, I know this code fails, but I still want to know what's going on inside it!" That's why I wondered maybe I should give more thought to how I'm using exceptions. (I'm raising a CLI::CommandError anywhere in a Command class where I know I don't want the CLI to proceed any further.) Cheers Ash * I'm sorry, I wrote the code on my own, my pair was actually taking his bank holiday off :) -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From ash.moran at patchspace.co.uk Mon Aug 29 17:31:35 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Mon, 29 Aug 2011 22:31:35 +0100 Subject: [rspec-users] Skipping slow specs in Guard but running them from that file Message-ID: Hi all I'm trying to optimise my spec run time. I have 123 examples so far, which run in ~4.2 seconds on average. But 116 of those will run in ~0.18 seconds. So, obviously, I only want to run the slow ones when I change that code. I've added `adapter: :slow` to the offending example group, which covers the whole of exactly one file. I've added this to the top of ./Guardfile: ENV["GUARD"] = "true" and this to my ./spec/spec_helper.rb: RSpec.configure do |config| config.filter_run(focus: true) config.filter_run_excluding(adapter: :slow) if GUARD config.run_all_when_everything_filtered = true end This is my terminal output: ===== Running: spec/bitcoin/data_access/satoshi/bdb_satoshi_wallet_repository_spec.rb No examples matched {:focus=>true}. Running all. No examples were matched. Perhaps {:adapter=>:slow} is excluding everything? 0 examples: 100% |==========================================| Time: 00:00:00 Finished in 0.00028 seconds 0 examples, 0 failures ===== This is not what I expected `config.run_all_when_everything_filtered = true` but from the Relish docs it looks like I misunderstood. Is this a bug, or a missing feature that could go into a future RSpec, or something that would not make sense anyway (i.e. `run_all_when_everything_filtered` really should only respect the tags from `filter_run`)? And more pragmatically, is there any way I can achieve what I want now, i.e. have that one file's examples run but only when I save it? Cheers Ash -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From ash.moran at patchspace.co.uk Mon Aug 29 18:34:59 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Mon, 29 Aug 2011 23:34:59 +0100 Subject: [rspec-users] Selectively running slow specs on file change with Guard Message-ID: <117728D2-0426-4F42-B701-DE1A00BC0C06@patchspace.co.uk> Hi all (again) Sorry for the new thread, but I don't have a copy of my own email in my inbox to reply to. Anyway I managed to cobble together a hack to make Guard filter slow specs by default, but unfilter them if the slow file itself was changed. I've Gisted the relevant sections of my Guardfile and spec_helper.rb[1]. It's not very elegant, and it will probably be possible to make a much better version when the "hook" branch[2] of Guard is merged into master. But in the mean time it's giving me near-instantaneous spec runs for my whole codebase. Admittedly it's still a small project at the moment, but at 659 examples/sec on my MBP, it has plenty of room to grow now? Hope that's of some use to somebody! Ash [1] https://gist.github.com/1179590 [2] https://github.com/guard/guard/tree/hook -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From elc at mingins.com Mon Aug 29 19:09:39 2011 From: elc at mingins.com (Shane Mingins) Date: Tue, 30 Aug 2011 11:09:39 +1200 Subject: [rspec-users] Testing a before_filter with a Rails.env condition Message-ID: Hi We have the occasional ApplicationController before_filter that is conditioned by the Rails.env and we like the following style: before_filter :check_for_something if Rails.env == 'production' And wish to spec this in an ApplicationController spec using an anonymous controller (e.g. http://relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller ) *Q. Just wondering how we can Rails.stub(:env).and_return('production') to trigger the before_filter?* As it stands we have moved the conditional down into the method and stub in the before(:each) e.g. before_filter :check_for_something def check_for_something if Rails.env == 'production' ... end end before(:each) do Rails.stub(:env).and_return('production') end Cheers Shane -------------- next part -------------- An HTML attachment was scrubbed... URL: From alex at stinky.com Mon Aug 29 19:09:50 2011 From: alex at stinky.com (Alex Chaffee) Date: Mon, 29 Aug 2011 16:09:50 -0700 Subject: [rspec-users] Selectively ignoring exceptions in examples In-Reply-To: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> References: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> Message-ID: On Mon, Aug 29, 2011 at 9:24 AM, Ash Moran wrote: > ? it "prints an error" do > ? ? ignoring_errors { > ? ? ? run_command(%w[ missing_wallet.dat ]) > ? ? } > ? ? stream_bundle.captured_error.should eq "Couldn't find wallet file: missing_wallet.dat\n" > ? end > > Now obviously that wouldn't be hard to add as a helper method. But it got me thinking? > > * Do any of you do this? I do. So often that I wrote a helper and put it in Wrong. I don't quite get what "stream_bundle.captured_error" is in your example, but I think the above example would become rescuing { run_command(%w[ missing_wallet.dat ]) }.message.should == "Couldn't find whatever" We've also got "capturing" for grabbing console output, e.g. capturing { puts "hi" }.should == "hi" or out, err = capturing(:stdout, :stderr) { ... } See https://github.com/sconover/wrong -- Alex Chaffee - alex at stinky.com http://alexch.github.com http://twitter.com/alexch From dchelimsky at gmail.com Mon Aug 29 22:34:31 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 29 Aug 2011 21:34:31 -0500 Subject: [rspec-users] Testing a before_filter with a Rails.env condition In-Reply-To: References: Message-ID: <92474AA2-A237-400D-AA5B-191450BB9A8B@gmail.com> On Aug 29, 2011, at 6:09 PM, Shane Mingins wrote: > Hi > > We have the occasional ApplicationController before_filter that is conditioned by the Rails.env and we like the following style: > > before_filter :check_for_something if Rails.env == 'production' This approach won't work because this line is eval'd once and only once when the file is loaded. > > And wish to spec this in an ApplicationController spec using an anonymous controller (e.g. http://relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller) > > Q. Just wondering how we can Rails.stub(:env).and_return('production') to trigger the before_filter? > > As it stands we have moved the conditional down into the method and stub in the before(:each) > > e.g. > > before_filter :check_for_something > > def check_for_something > if Rails.env == 'production' > ... > end > end > > before(:each) do > Rails.stub(:env).and_return('production') > end This approach works because Rails.env inside the check_for_something method is eval'd each time the method is called. HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From elc at mingins.com Mon Aug 29 23:27:18 2011 From: elc at mingins.com (Shane Mingins) Date: Tue, 30 Aug 2011 15:27:18 +1200 Subject: [rspec-users] Testing a before_filter with a Rails.env condition In-Reply-To: <92474AA2-A237-400D-AA5B-191450BB9A8B@gmail.com> References: <92474AA2-A237-400D-AA5B-191450BB9A8B@gmail.com> Message-ID: Yeah sorry I realised that was what was happening. But thought I'd ask the question in case there might have been something I was missing (in the docs) or a smart hack ;-) On 30 August 2011 14:34, David Chelimsky wrote: > On Aug 29, 2011, at 6:09 PM, Shane Mingins wrote: > > Hi > > We have the occasional ApplicationController before_filter that is > conditioned by the Rails.env and we like the following style: > > before_filter :check_for_something if Rails.env == 'production' > > > This approach won't work because this line is eval'd once and only once > when the file is loaded. > > > And wish to spec this in an ApplicationController spec using an anonymous > controller (e.g. > http://relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller > ) > > *Q. Just wondering how we can Rails.stub(:env).and_return('production') to > trigger the before_filter?* > > As it stands we have moved the conditional down into the method and stub in > the before(:each) > > e.g. > > before_filter :check_for_something > > def check_for_something > if Rails.env == 'production' > ... > end > end > > before(:each) do > Rails.stub(:env).and_return('production') > end > > > This approach works because Rails.env inside the check_for_something method > is eval'd each time the method is called. > > HTH, > David > > _______________________________________________ > 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 forum at erisiandiscord.de Tue Aug 30 02:01:28 2011 From: forum at erisiandiscord.de (Nikolay Sturm) Date: Tue, 30 Aug 2011 08:01:28 +0200 Subject: [rspec-users] Skipping slow specs in Guard but running them from that file In-Reply-To: References: Message-ID: <20110830060127.GA3208@erisiandiscord.de> * Ash Moran [2011-08-30]: > I'm trying to optimise my spec run time. I have 123 examples so far, > which run in ~4.2 seconds on average. But 116 of those will run in > ~0.18 seconds. So, obviously, I only want to run the slow ones when I > change that code. I have a similar situation with the slow specs being integration specs in a special directory. I tagged all those example groups and setup two guards. The first is for unit tests and ignores all examples tagged 'integration' and doesn't watch spec/integration. The second guard just watches spec/integration and runs only examples tagged 'integration'. Haven't used it much yet, though. cheers, Nikolay From ash.moran at patchspace.co.uk Tue Aug 30 06:20:53 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Tue, 30 Aug 2011 11:20:53 +0100 Subject: [rspec-users] Testing console IO / Re: Selectively ignoring exceptions in examples In-Reply-To: References: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> Message-ID: On 30 Aug 2011, at 00:09, Alex Chaffee wrote: > I do. So often that I wrote a helper and put it in Wrong. Cool, and also? I really should try Wrong! I've just put it on my project TODO list as something to investigate > I don't quite get what "stream_bundle.captured_error" is in your > example, but I think the above example would become > > rescuing { > run_command(%w[ missing_wallet.dat ]) > }.message.should == "Couldn't find whatever" > > We've also got "capturing" for grabbing console output, e.g. > > capturing { puts "hi" }.should == "hi" Sorry, I didn't realise how opaque that is if you don't know what one is. Basically, I wanted to decouple the app from STDIN/STDOUT/STDERR so I started injecting StringIO objects around for testing. After a while I had a data clump of @input/@output/@error in classes everywhere, so I made a StreamBundle[1] to encapsulate them. (Apologies for lack of syntax highlighting on patch-tag.com) But then I realised my tests were making a `StreamBundle.new(StringIO.new, StringIO.new, StringIO.new)` everywhere, so I factored out the duplication into a CapturingStreamBundle[2] decorator that records all output to secondary StringIO objects. Now I just call `CapturingStreamBundle.test_bundle` to get one. I've found this pattern useful as now that only files in my bin/ folder ever access ARGV, STDOUT etc, the code is more loosely coupled, but also I can see exactly where I'm talking to the outside world. Every time I want to send some output, I think- "Should I really be giving this object a StreamBundle? Is it really appropriate for it to be talking to the user directly?" That's the backstory anyway! Ash [1] https://patch-tag.com/r/ashmoran/rbcoin/snapshot/current/content/pretty/lib/bitcoin/console/stream_bundle.rb [2] https://patch-tag.com/r/ashmoran/rbcoin/snapshot/current/content/pretty/lib/bitcoin/console/capturing_stream_bundle.rb -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From ash.moran at patchspace.co.uk Tue Aug 30 06:29:17 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Tue, 30 Aug 2011 11:29:17 +0100 Subject: [rspec-users] Skipping slow specs in Guard but running them from that file In-Reply-To: <20110830060127.GA3208@erisiandiscord.de> References: <20110830060127.GA3208@erisiandiscord.de> Message-ID: <97DA81BD-A160-4EA8-A437-C7BCD1642557@patchspace.co.uk> On 30 Aug 2011, at 07:01, Nikolay Sturm wrote: > I have a similar situation with the slow specs being integration specs > in a special directory. I tagged all those example groups and setup two > guards. The first is for unit tests and ignores all examples tagged > 'integration' and doesn't watch spec/integration. The second guard just > watches spec/integration and runs only examples tagged 'integration'. > Haven't used it much yet, though. I never thought of that! Yes, that could also work, probably better in fact. It just involves running multiple Guard processes, although there's Terminitor[1] for that! At this point I didn't want to separate my specs into unit/ and integration/ folders. One surprisingly major reason is that I live off the RSpec TextMate bundle's Alternate File feature to swap between lib/ and spec/ files. It also makes writing the Guard matchers a lot easier (i.e. you don't have to change anything). I may change that if I find I often want to locate files by their nature. Cheers Ash [1] https://rubygems.org/gems/terminitor -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From forum at erisiandiscord.de Tue Aug 30 09:45:37 2011 From: forum at erisiandiscord.de (Nikolay Sturm) Date: Tue, 30 Aug 2011 15:45:37 +0200 Subject: [rspec-users] Skipping slow specs in Guard but running them from that file In-Reply-To: <97DA81BD-A160-4EA8-A437-C7BCD1642557@patchspace.co.uk> References: <20110830060127.GA3208@erisiandiscord.de> <97DA81BD-A160-4EA8-A437-C7BCD1642557@patchspace.co.uk> Message-ID: <20110830134537.GB3208@erisiandiscord.de> * Ash Moran [2011-08-30]: > I never thought of that! Yes, that could also work, probably better in > fact. It just involves running multiple Guard processes, although > there's Terminitor[1] for that! A single guard process is enough, it will start all guards defined in your Guardfile. cheers, Nikolay -- "It's all part of my Can't-Do approach to life." Wally From ash.moran at patchspace.co.uk Tue Aug 30 10:41:18 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Tue, 30 Aug 2011 15:41:18 +0100 Subject: [rspec-users] Skipping slow specs in Guard but running them from that file In-Reply-To: <20110830134537.GB3208@erisiandiscord.de> References: <20110830060127.GA3208@erisiandiscord.de> <97DA81BD-A160-4EA8-A437-C7BCD1642557@patchspace.co.uk> <20110830134537.GB3208@erisiandiscord.de> Message-ID: <7EB47CBD-AC71-43CB-8E01-8EE8E0E1276A@patchspace.co.uk> On 30 Aug 2011, at 14:45, Nikolay Sturm wrote: > A single guard process is enough, it will start all guards defined in > your Guardfile. I did not know that! I'm still new to Guard, a recent convert from Autotest. Thanks for the tip. Cheers Ash -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From alex at stinky.com Tue Aug 30 14:24:36 2011 From: alex at stinky.com (Alex Chaffee) Date: Tue, 30 Aug 2011 11:24:36 -0700 Subject: [rspec-users] Testing console IO / Re: Selectively ignoring exceptions in examples In-Reply-To: References: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> Message-ID: > I've found this pattern useful as now that only files in my bin/ folder ever access ARGV, STDOUT etc, the code is more loosely coupled, but also I can see exactly where I'm talking to the outside world. Every time I want to send some output, I think- "Should I really be giving this object a StreamBundle? Is it really appropriate for it to be talking to the user directly?" I hear you. When I first started coding in Ruby I was injecting all over the place. But nowadays I'm much more comfortable with the flexibility of things like reopening classes during tests, or mocking methods, or resetting globals... ...or grabbing and reassigning $stdout and $stderr, which is what "capturing" does. The basic idea is that Ruby is *already* decoupled from stdin/out/err via its dynamic nature and $globals. I get that by naming the inputs explicitly you're ensuring 100% compliance but you should consider whether it's worth it. Passing around DI clumps (aka "value objects" or "parameter objects" in some circles) can make your code a lot less concise, and concision is one of the great joys of Ruby. btw apologies if you already know this, but inside a normal Ruby program you should always use $stderr/$stdout/$stdin, not STDERR/STDOUT/STDIN since the former are settable and the latter are hardcoded to the "real" streams and, as true CONSTANTS, not easy to change. For whatever reason I see the caps versions used a lot more than the dollar versions, which is a shame. -- Alex Chaffee - alex at stinky.com http://alexch.github.com http://twitter.com/alexch From ash.moran at patchspace.co.uk Tue Aug 30 16:59:37 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Tue, 30 Aug 2011 21:59:37 +0100 Subject: [rspec-users] Testing console IO / Re: Selectively ignoring exceptions in examples In-Reply-To: References: <1B5EB0F3-95E7-48C9-BB82-744EA22C32A8@patchspace.co.uk> Message-ID: On 30 Aug 2011, at 19:24, Alex Chaffee wrote: > ...or grabbing and reassigning $stdout and $stderr, which is what > "capturing" does. > > The basic idea is that Ruby is *already* decoupled from stdin/out/err > via its dynamic nature and $globals. I get that by naming the inputs > explicitly you're ensuring 100% compliance but you should consider > whether it's worth it. Passing around DI clumps (aka "value objects" > or "parameter objects" in some circles) can make your code a lot less > concise, and concision is one of the great joys of Ruby. Yes, I've done constant reassigning in the past, with before/after blocks to control the environment. I wasn't specifically avoiding that here, it's just what fell out my my initial design to pass the standard streams in as variables from the bin/ command. I'm going to see how it pans out, as I've never been so strict about IO before. > btw apologies if you already know this, but inside a normal Ruby > program you should always use $stderr/$stdout/$stdin, not > STDERR/STDOUT/STDIN since the former are settable and the latter are > hardcoded to the "real" streams and, as true CONSTANTS, not easy to > change. For whatever reason I see the caps versions used a lot more > than the dollar versions, which is a shame. Actually I didn't know that, so thanks for pointing it out :-) Although I have often been puzzled by the presence of both forms? Cheers Ash -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From qrprat77 at gmail.com Wed Aug 31 04:22:14 2011 From: qrprat77 at gmail.com (GB Hoyt) Date: Wed, 31 Aug 2011 04:22:14 -0400 Subject: [rspec-users] Getting Desired behaviour when I run the program, but spec still fails. Message-ID: <4E5DEF36.7040704@gmail.com> I'm not doing something right with the following spec and code: https://gist.github.com/1183066 My problem lies in the last Spec: it "should load the contents of the text file into the action list" do @menu.action_list.empty?.should == false @menu.display_menu(@messenger) end My currently pain rattled body and mind is not processing the problem very well. I suspect I'm not writing the spec right, any help? Thanks, GB Hoyt Lakeland, FL From ash.moran at patchspace.co.uk Wed Aug 31 07:09:21 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Wed, 31 Aug 2011 12:09:21 +0100 Subject: [rspec-users] Getting Desired behaviour when I run the program, but spec still fails. In-Reply-To: <4E5DEF36.7040704@gmail.com> References: <4E5DEF36.7040704@gmail.com> Message-ID: <2A48E51E-0DE8-45F6-A301-4A5C9F8086E8@patchspace.co.uk> On 31 Aug 2011, at 09:22, GB Hoyt wrote: > I'm not doing something right with the following spec and code: > https://gist.github.com/1183066 > > My problem lies in the last Spec: > > it "should load the contents of the text file into the action list" do > @menu.action_list.empty?.should == false > @menu.display_menu(@messenger) > end My currently pain rattled body and mind is not processing the > problem very well. I suspect I'm not writing the spec right, any help? From just a quick skim over, these two examples look contradictory: it "should throw an error if asked to display a menu that does not exist" @menu.display_menu(@messenger).should == "#{@menu.txtfile} does not exist!" end #Here's my problem child it "should load the contents of the text file into the action list" do @menu.action_list.empty?.should == false @menu.display_menu(@messenger) end Do you need two separate contexts, one with `Menu.new("Title_test_real.txt")`? BTW? s/\t/ /g ;) (Conventional Ruby indents are 2 spaces, tabs make it harder to read for people used to that) HTH, if not, maybe someone else can see the bug. Ash -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From qrprat77 at gmail.com Wed Aug 31 08:57:55 2011 From: qrprat77 at gmail.com (GB Hoyt) Date: Wed, 31 Aug 2011 08:57:55 -0400 Subject: [rspec-users] Getting Desired behaviour when I run the program, but spec still fails. In-Reply-To: <2A48E51E-0DE8-45F6-A301-4A5C9F8086E8@patchspace.co.uk> References: <4E5DEF36.7040704@gmail.com> <2A48E51E-0DE8-45F6-A301-4A5C9F8086E8@patchspace.co.uk> Message-ID: <4E5E2FD3.3090708@gmail.com> On 08/31/2011 07:09 AM, Ash Moran wrote: > > >From just a quick skim over, these two examples look contradictory: > > it "should throw an error if asked to display a menu that does not exist" > @menu.display_menu(@messenger).should == "#{@menu.txtfile} does not exist!" > end > > #Here's my problem child > it "should load the contents of the text file into the action list" do > @menu.action_list.empty?.should == false > @menu.display_menu(@messenger) > end > yeah, I realized that shortly after posting it, and I modified the spec as such: #Here's the spec: module Antlers describe Menu do context "display a Menu from a text file" do before (:each) do @messenger = mock("messenger").as_null_object @menu = Menu.new("Main Menu") end #before ... #This spec passes it "should throw an error if asked to display a menu that does not exist" do menu2 = Menu.new("Title_Menu") menu2.display_menu(@messenger).should == "#{menu2.txtfile}does not exist!" end #this spec does not pass, the object is initialized with an empty hash, the display_menu method #populates that hash. That's how it behaves in usage it "should load the contents of the text file into the action list" do @menu.action_list.empty?.should == false @menu.display_menu(@messenger) end end #new menu context end #menu end #module I wonder if it has something to do with @messenger being a mock null object? > Do you need two separate contexts, one with `Menu.new("Title_test_real.txt")`? > > BTW? s/\t/ /g ;) (Conventional Ruby indents are 2 spaces, tabs make it harder to read for people used to that) > > HTH, if not, maybe someone else can see the bug. > > Ash > > bah, I always forget to check how geany marks tabs. I fixt it! From johnf at bitsbuilder.com Wed Aug 31 18:06:45 2011 From: johnf at bitsbuilder.com (John Feminella) Date: Wed, 31 Aug 2011 18:06:45 -0400 Subject: [rspec-users] Better visualizations of spec running time besides --profile? Message-ID: We have about 2,000 specs in a Rails app that take roughly 80 seconds to run, and I'm trying to improve the performance of things a bit. While the profile mode has proven useful so far, it only shows the top ten slowest specs. Unfortunately, we have lot of specs, and we've picked off all the low-hanging fruit -- the ones remaining are all < ~0.1 sec or less. I'd like to streamline things further by seeing if there's a way to get information about slow spec *files* (not just individual specs), because I suspect that slower specs will be next to other slow specs. Any ideas about how I can get this information, or do I need to roll my own benchmarker? ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ From julian at leviston.net Wed Aug 31 21:40:34 2011 From: julian at leviston.net (Julian Leviston) Date: Thu, 1 Sep 2011 11:40:34 +1000 Subject: [rspec-users] Better visualizations of spec running time besides --profile? In-Reply-To: References: Message-ID: Concurrency? Julian On 01/09/2011, at 8:06 AM, John Feminella wrote: > We have about 2,000 specs in a Rails app that take roughly 80 seconds > to run, and I'm trying to improve the performance of things a bit. > > While the profile mode has proven useful so far, it only shows the top > ten slowest specs. Unfortunately, we have lot of specs, and we've > picked off all the low-hanging fruit -- the ones remaining are all < > ~0.1 sec or less. I'd like to streamline things further by seeing if > there's a way to get information about slow spec *files* (not just > individual specs), because I suspect that slower specs will be next to > other slow specs. > > Any ideas about how I can get this information, or do I need to roll > my own benchmarker? > > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From chabgood at gmail.com Sun Aug 28 22:36:39 2011 From: chabgood at gmail.com (Chris Habgood) Date: Sun, 28 Aug 2011 19:36:39 -0700 (PDT) Subject: [rspec-users] rails 3.1 with guard, not detecting changes Message-ID: <3426375.1065.1314585399537.JavaMail.geo-discussion-forums@yqgn17> I am using guard to detect changes and run rspec automatically. It doe snot seem to see my changes in a model when guard is run. When I run "rake spec" from the cli rspec runs fine. Does anyone know why this might be happening? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ash.moran at patchspace.co.uk Mon Aug 29 08:08:56 2011 From: ash.moran at patchspace.co.uk (Ash Moran) Date: Mon, 29 Aug 2011 13:08:56 +0100 Subject: [rspec-users] Selectively ignoring exceptions in examples Message-ID: <979DDA26-FF9C-4559-818D-43A5B59A167A@patchspace.co.uk> Hi all Long time since I've posted here, hope you're all well :-) I have a question about ignoring exceptions when they're not interesting. For example, I have a few cases in my code along these lines? it "prints an error" do expect { run_command(%w[ missing_wallet.dat ]) }.to raise_error stream_bundle.captured_error.should eq "Couldn't find wallet file: missing_wallet.dat\n" end it "raises a CLI::CommandError" do expect { run_command(%w[ missing_wallet.dat ]) }.to raise_error(CLI::CommandError) end But in the first example, I'm only bothered about the output, not the error. So I was thinking of writing something along the lines of: it "prints an error" do ignoring_errors { run_command(%w[ missing_wallet.dat ]) } stream_bundle.captured_error.should eq "Couldn't find wallet file: missing_wallet.dat\n" end Now obviously that wouldn't be hard to add as a helper method. But it got me thinking? * Do any of you do this? * Does RSpec already let you somehow? * Is it a useful convention? * Is it hiding anything else? (I don't use exceptions much, so I may be abusing them.) Cheers Ash -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashmoran From nick at deadorange.com Mon Aug 29 11:33:16 2011 From: nick at deadorange.com (Nick) Date: Mon, 29 Aug 2011 08:33:16 -0700 (PDT) Subject: [rspec-users] What causes "undefined method `each_pair'"? Message-ID: <1220926.5683.1314631996267.JavaMail.geo-discussion-forums@yqah42> Hey folks. I've been running into this problem a lot lately, and haven't been able to figure out what's causing it, nor how to fix it: Failure/Error: @importer = SpreadsheetImporter.new @catalog, @excel, @photos_dir NoMethodError: undefined method `each_pair' for # # ./app/models/spreadsheet_importer.rb:15:in `initialize' # ./spec/models/spreadsheet_importer_spec.rb:25:in `new' # ./spec/models/spreadsheet_importer_spec.rb:25:in `block (2 levels) in ' Line 15 of spreadsheet_importer.rb is: @products_importer = SpreadsheetImporter::Products.new catalog, spreadsheet Line 25 of spreadsheet_importer_spec.rb is: @importer = SpreadsheetImporter.new @catalog, @excel, @photos_dir What perplexes me is that I've stubbed out the call to SpreadsheetImporter::Products.new , so the class' initializer isn't being called. Here're some more complete code snippets: https://gist.github.com/1178636 Any help would be greatly appreciated. Thanks! Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: From rahul.baxi at gmail.com Mon Aug 29 19:16:46 2011 From: rahul.baxi at gmail.com (rahul baxi) Date: Mon, 29 Aug 2011 16:16:46 -0700 Subject: [rspec-users] Redirect App for RSpec 2 Message-ID: Hi, My name is Rahul and I'm an RoR developer. I would like to volunteer and write an app to manage redirects on the rspec.info site. Please let me know if anyone else hasn't taken this up already. Thanks, Rahul -------------- next part -------------- An HTML attachment was scrubbed... URL: From srini.srini39 at gmail.com Tue Aug 30 13:57:20 2011 From: srini.srini39 at gmail.com (srini 1971) Date: Tue, 30 Aug 2011 23:27:20 +0530 Subject: [rspec-users] write an app to manage redirects Message-ID: Hi Can i be allowed to "write an app to manage redirects" Regards, Srinivasan -------------- next part -------------- An HTML attachment was scrubbed... URL: From fedegl at gmail.com Tue Aug 30 22:23:17 2011 From: fedegl at gmail.com (Federico Gonzalez) Date: Tue, 30 Aug 2011 19:23:17 -0700 (PDT) Subject: [rspec-users] Anonymous controller not working Message-ID: <30485649.265.1314757397604.JavaMail.geo-discussion-forums@prdr3> I have the following files. # application_controller_spec.rb require 'spec_helper' describe ApplicationController do controller do def index raise CanCan::AccessDenied.new("Not authorized!", :read, Order) end end describe "rescue from AccessDenied" do it "should rescue from CanCan::AccessDenied" do get :index response.should redirect_to("/") end end end # application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery rescue_from CanCan::AccessDenied do |exception| redirect_to root_url, :alert => exception.message end end But when I run the spec get the following error: Failure/Error: response.should redirect_to("/") Expected response to be a <:redirect>, but was <200> -------------- next part -------------- An HTML attachment was scrubbed... URL: