From patrick at collinatorstudios.com Tue May 1 03:24:31 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Mon, 30 Apr 2012 20:24:31 -0700 (PDT) Subject: [rspec-users] stuck in testing hell... Message-ID: Hey everyone, So here I am, trying to love my testing process-- but I really hit a wall today and experienced a moment where testing totally causes a lack of productivity... To make a long story short, here's what happened: I've been integrating a 3rd party payment service within my app-- The way it works is, you get a token for the transaction, and then send a request to their API to see if the credit card authorizes, and then if it does, you can purchase it and make sure that's valid... So I have a purchase model, and I am using state_machine to store the transaction state... For example, when an attempt to authorize a credit card is done, if that fails, then the state machine transitions within a failure block to set the purchase's state to "authorization_failed". ... Ok... So, I made tests to cover all of this behavior, and everything was perfect-- all tests were passing, and then I was told: "Hey, we want to store the transaction data in the purchase records-- whether it failed or succeded"... I said: Sure! No Problem! The way I had this setup was, an attr_accessor :samurai_transaction and then a before_create callback that calls a method :build_samurai_transaction ... That method basically did: Samurai::Processor.authorize( ...bunch o' auth data stuff... ) So to keep this data around, I made a migration to create the column (same name as the attr_accessor property) and then I added serialize :samurai_transaction and removed the attr_accessor macro. Everything should be exactly the same behaviorally.. Ran my tests to verify, and.. FAIL FAIL FAIL FAIL FAIL FAIL FAIL... Inspecting the failures, I see that all of a sudden I am getting bogus errors about "undefined method `matches_method?' for nil:NilClass" I go through everything trying to figure out where this is coming from and finally find that it's from my stubbing out the calls to Samurai.. I was doing: auth = stub("auth", :success? => true, :capture => true) Samurai::Processor.stubs(:authorize).returns auth .. It worked fine before, but apparently something can't handle the serialized database column holding a mocked object? No idea... So I changed this to: auth = OpenStruct.new(:success? => true, :capture => true) The error went away, which then made me convinced that it is something to do with Mocha..... However, then I saw other tests failing, and guess why? WRONG NUMBER OF ARGUMENTS FOR CAPTURE (0 for 1). .... uhh? Samurai has it's own ".capture" method to take funds.. This has some conflicts with Rails' internal ".capture"......... For some reason, o = OpenStruct.new(:capture => true) o.capture => wrong number of arguments error... So now I have to do: o = OpenStruct.new(:kapture => true) def o.capture kapture end and, suddenly I am finding myself in hacky hack land-- and hating every minute of it. Not to mention my tests still are breaking because of some other undetermined problems having to do with OpenStruct not being as "simple" of a solution as I was hoping. ... AAAAAAAAAAAAAAAAAAAAAAGH! Patrick J. Collins http://collinatorstudios.com From apremdas at gmail.com Tue May 1 11:37:06 2012 From: apremdas at gmail.com (Andrew Premdas) Date: Tue, 1 May 2012 12:37:06 +0100 Subject: [rspec-users] stuck in testing hell... In-Reply-To: References: Message-ID: On 1 May 2012 04:24, Patrick J. Collins wrote: > Hey everyone, > > So here I am, trying to love my testing process-- but I really hit a > wall today and experienced a moment where testing totally causes a lack > of productivity... To make a long story short, here's what happened: > > I've been integrating a 3rd party payment service within my app-- The > way it works is, you get a token for the transaction, and then send a > request to their API to see if the credit card authorizes, and then if > it does, you can purchase it and make sure that's valid... > > So I have a purchase model, and I am using state_machine to store the > transaction state... For example, when an attempt to authorize a > credit card is done, if that fails, then the state machine transitions > within a failure block to set the purchase's state to > "authorization_failed". > > ... Ok... So, I made tests to cover all of this behavior, and > everything was perfect-- all tests were passing, and then I was told: > "Hey, we want to store the transaction data in the purchase records-- > whether it failed or succeded"... I said: Sure! No Problem! > > The way I had this setup was, an attr_accessor :samurai_transaction > > and then a before_create callback that calls a method > :build_samurai_transaction > > ... That method basically did: > > Samurai::Processor.authorize( ...bunch o' auth data stuff... ) > > So to keep this data around, I made a migration to create the column > (same name as the attr_accessor property) and then I added serialize > :samurai_transaction and removed the attr_accessor macro. > > Everything should be exactly the same behaviorally.. Ran my tests to > verify, and.. FAIL FAIL FAIL FAIL FAIL FAIL FAIL... > > Inspecting the failures, I see that all of a sudden I am getting bogus > errors about "undefined method `matches_method?' for nil:NilClass" > > I go through everything trying to figure out where this is coming from > and finally find that it's from my stubbing out the calls to Samurai.. > I was doing: > > auth = stub("auth", :success? => true, :capture => true) > Samurai::Processor.stubs(:authorize).returns auth > > .. It worked fine before, but apparently something can't handle the > serialized database column holding a mocked object? No idea... > > So I changed this to: > auth = OpenStruct.new(:success? => true, :capture => true) > > The error went away, which then made me convinced that it is something > to do with Mocha..... However, then I saw other tests failing, and > guess why? > > WRONG NUMBER OF ARGUMENTS FOR CAPTURE (0 for 1). > > .... uhh? > > Samurai has it's own ".capture" method to take funds.. This has some > conflicts with Rails' internal ".capture"......... For some reason, > o = OpenStruct.new(:capture => true) > o.capture > => wrong number of arguments error... > > So now I have to do: > > o = OpenStruct.new(:kapture => true) > def o.capture > kapture > end > > and, suddenly I am finding myself in hacky hack land-- and hating every > minute of it. > > Not to mention my tests still are breaking because of some other > undetermined > problems having to do with OpenStruct not being as "simple" of a > solution as I was hoping. > > ... AAAAAAAAAAAAAAAAAAAAAAGH! > > > Patrick J. Collins > http://collinatorstudios.com > > The frustration your experiencing is actually a good thing, its your brain saying "hey something is just not right". Of course our natural reaction to this is just to get angry, but if we can get past that there is an opportunity to learn something important. In my limited experience I've found that listening to tests is one of the best ways to learn. There is probably something seriously wrong with your existing tests and code. So instead of trying to work around it, and get your tests to pass, seize the opportunity and try and work out why your tests and code suck and get to the bottom of the problem. This is a real opportunity to learn something. Unfortunately I'm not bright enough to tell you whats wrong, but applying general principles of good OO and good testing should help. In particular things like 1. ensuring your tests say what they are testing 2. ensure your unit tests only test responsibilities of their object instance 3. look for smells (e.g. tests that require lots of mocks are bad tests, and are indicative of highly coupled code), ... Finally an important lesson is that your tests being green is really not that important. Its much more important that tests 1. say what they do 2. are listened to, so they exert a positive influence on your design 3. make things easy to fix when they go red All best Andrew _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick at collinatorstudios.com Tue May 1 19:17:18 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Tue, 1 May 2012 12:17:18 -0700 (PDT) Subject: [rspec-users] stuck in testing hell... In-Reply-To: References: Message-ID: > The frustration your experiencing is actually a good thing, its your brain saying "hey something is just not right". Of > course our natural reaction to this is just to get angry, but if we can get past that there is an opportunity to learn > something important. In my limited experience I've found that listening to tests is one of the best ways to learn. There > is probably something seriously wrong with your existing tests and code. So instead of trying to work around it, and get > your tests to pass, seize the opportunity and try and work out why your tests and code suck and get to the bottom of the > problem. This is a real opportunity to learn something. I hear you, but honestly these tests are very straight forward and not overly complicated in any way, shape, or form. I did some more experimenting and was able to narrow the problem down to: Serialization and Mocha. class Foo < ActiveRecord::Base serialize :bar end by using debugger within a test, I did: > foo = Foo.create! > foo.save => true > foo.bar = stub("waka waka") > foo.save => NoMethodError: undefined method `matches_method?' for nil:NilClass from /Users/bountybuy/.rvm/gems/ruby-1.9.3-p125 at bountybuy/gems/mocha-0.10.5/lib/mocha/mock.rb:185:in `respond_to?' ... So the solution since using OpenStruct as an alternative was a disaster is: Foo.class_eval { attr_accessor :bar } With that, ActiveRecord no longer handles the setter/getter methods for that column, and this issue goes away. AAAAAAAAAAAAGH! So I am out of testing hell now.. yay! Patrick J. Collins http://collinatorstudios.com From dchelimsky at gmail.com Tue May 1 19:55:03 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 1 May 2012 14:55:03 -0500 Subject: [rspec-users] stuck in testing hell... In-Reply-To: References: Message-ID: On Tue, May 1, 2012 at 2:17 PM, Patrick J. Collins wrote: >> The frustration your experiencing is actually a good thing, its your brain saying "hey something is just not right". Of >> course our natural reaction to this is just to get angry, but if we can get past that there is an opportunity to learn >> something important. In my limited experience I've found that listening to tests is one of the best ways to learn. There >> is probably something seriously wrong with your existing tests and code. So instead of trying to work around it, and get >> your tests to pass, seize the opportunity and try and work out why your tests and code suck and get to the bottom of the >> problem. This is a real opportunity to learn something. > > I hear you, but honestly these tests are very straight forward and not > overly complicated in any way, shape, or form. ?I did some more > experimenting and was able to narrow the problem down to: ?Serialization and Mocha. Tests have to run code. Your tests are running already complicated code (Rails), that is further complicated by introducing Serialization, and then further complicated by using Mocha. I do these things too, so I'm not saying they are bad, but they are nothing if they are not un-straight-forward and complicated. Just because syntax hides all that complexity from you doesn't make it go away. FWIW, David From dchelimsky at gmail.com Fri May 4 01:37:39 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 3 May 2012 20:37:39 -0500 Subject: [rspec-users] rspec-2.10 is released! Message-ID: <26F528B93AEE43C5AC094210994D2C35@gmail.com> rspec-2.10 is released! Cucumber docs http://rubydoc.info/gems/rspec-core http://rubydoc.info/gems/rspec-expectations http://rubydoc.info/gems/rspec-mocks http://rubydoc.info/gems/rspec-rails API Docs (RDoc) http://relishapp.com/gems/rspec-core http://relishapp.com/gems/rspec-expectations http://relishapp.com/gems/rspec-mocks http://relishapp.com/gems/rspec-rails ### rspec-core-2.10.0 full changelog: http://github.com/rspec/rspec-core/compare/v2.9.0...v2.10.0 Enhancements * Add `prepend_before` and `append_after` hooks (preethiramdev) * intended for extension libs * restores rspec-1 behavior * Reporting of profiled examples (moro) * Report the total amount of time taken for the top slowest examples. * Report what percentage the slowest examples took from the total runtime. Bug fixes * Properly parse `SPEC_OPTS` options. * `example.description` returns the location of the example if there is no explicit description or matcher-generated description. * RDoc fixes (Grzegorz ?wirski) * Do not modify example ancestry when dumping errors (Michael Grosser) ### rspec-expectations-2.10.0 full changelog: http://github.com/rspec/rspec-expectations/compare/v2.9.1...v2.10.0 Enhancements * Add new `start_with` and `end_with` matchers (Jeremy Wadsack) * Add new matchers for specifying yields (Myron Marson): * `expect {...}.to yield_control` * `expect {...}.to yield_with_args(1, 2, 3)` * `expect {...}.to yield_with_no_args` * `expect {...}.to yield_successive_args(1, 2, 3)` * `match_unless_raises` takes multiple exception args Bug fixes * Fix `be_within` matcher to be inclusive of delta. * Fix message-specific specs to pass on Rubinius (John Firebaugh) ### rspec-mocks-2.10.0 full changelog: http://github.com/rspec/rspec-mocks/compare/v2.9.0...v2.10.0 Bug fixes * fail fast when an `exactly` or `at_most` expectation is exceeded ### rspec-rails-2.10.0 full changelog: http://github.com/rspec/rspec-core/compare/v2.9.0...v2.10.0 Bug fixes * `render_views` called in a spec can now override the config setting. (martinsvalin) * Fix `render_views` for anonymous controllers on 1.8.7. (hudge, mudge) * Eliminate use of deprecated `process_view_paths` * Fix false negatives when using `route_to` matcher with `should_not` * `controller` is no longer nil in `config.before` hooks * Change `request.path_parameters` keys to symbols to match real Rails environment (Nathan Broadbent) * Silence deprecation warnings in pre-2.9 generated view specs (Jonathan del Strother) -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick at collinatorstudios.com Fri May 4 20:07:47 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Fri, 4 May 2012 13:07:47 -0700 (PDT) Subject: [rspec-users] rspec 2.10.0 breaks controller specs... Message-ID: Apparently 2.10.0 doesn't like this line in devise's lib/devise/test_helpers.rb @request.env['warden'] = Warden::Proxy.new(@request.env, manager) ... This worked fine in 2.8.1, but now all my controller specs give me: Failure/Error: Unable to find matching line from backtrace NoMethodError: undefined method `env' for nil:NilClass # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/devise-2.0.4/lib/devise/test_helpers.rb:33:in `warden' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-rails-2.10.0/lib/rspec/rails/adapters.rb:15:in `block (2 levels) in setup' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:178:in `instance_eval' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:178:in `instance_eval' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:23:in `run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:63:in `block in run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:63:in `each' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:63:in `run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/hooks.rb:400:in `run_hook' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:298:in `run_before_each_hooks' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:239:in `run_before_each' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:86:in `block in run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:195:in `with_around_each_hooks' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example.rb:84:in `run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:353:in `block in run_examples' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:349:in `map' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:349:in `run_examples' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:335:in `run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:336:in `block in run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/r 17 def process(*) spec/core/example_group.rb:336:in `map' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/example_group.rb:336:in `run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:28:in `map' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:28:in `block in run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/reporter.rb:34:in `report' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/command_line.rb:25:in `run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/runner.rb:69:in `run' # /Users/patrick/.rvm/gems/ruby-1.9.3-p125 at my_app/gems/rspec-core-2.10.0/lib/rspec/core/runner.rb:10:in `block in autorun' Patrick J. Collins http://collinatorstudios.com From dchelimsky at gmail.com Fri May 4 20:10:13 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 4 May 2012 15:10:13 -0500 Subject: [rspec-users] rspec 2.10.0 breaks controller specs... In-Reply-To: References: Message-ID: On Fri, May 4, 2012 at 3:07 PM, Patrick J. Collins wrote: > Apparently 2.10.0 doesn't like this line in devise's lib/devise/test_helpers.rb > ?@request.env['warden'] = Warden::Proxy.new(@request.env, manager) Please file bug reports to https://github.com/rspec/rspec-rails/issues, and please look to see if someone else already has before you do: https://github.com/rspec/rspec-rails/issues/534 Cheers, David From patrick at collinatorstudios.com Fri May 4 23:14:50 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Fri, 4 May 2012 16:14:50 -0700 (PDT) Subject: [rspec-users] Preferred way to predict behavior in controller spec? In-Reply-To: References: Message-ID: So, I have an action like this: def destroy if purchase.user == current_user && purchase.refund redirect_to purchase else flash[:error] = t("purchases.refund.failure") render :show end end ... I wanted a controller test to verify the following: 1. It's the correct user 2. The refund was successful 3. User is redirected ... Initially, I thought this would go something like this: describe "#destroy" do let(:purchase) { create_purchase } it "refunds the buyer" do subject.stubs(:current_user).returns purchase.user purchase.stubs(:refund).returns true put :destroy, { :id => purchase.id } response.should be_redirect end end WRONG!!!!!!!!! Obviously, that is not cool because purchase.stubs(:refund) will not be the right object in the context of the controller...... So since my controller is actually doing the following: def destroy if purchase.user # etc... end private def purchase @purchase ||= Purchase.find(params[:id]) end I could do: subject.stubs(:purchase).returns(purchase) .. However, now that kind of defeats the purpose of put :destroy, { :id => purchase.id }... So I didn't really like that-- I am no longer verifying it's the right record, so maybe the things I wanted to test were actually: 1. It finds the purchase 2. It's the correct user 3. The refund was successful 4. User is redirected ........ So I ended up making my test do: describe "#destroy" do let(:purchase) { create_purchase } def do_destroy put :destroy, { :id => purchase.id } end it "refunds the buyer" do subject.stubs(:current_user).returns purchase.user Purchase.any_instance.stubs(:refund).returns true do_destroy response.should be_redirect end subject.stubs(:current_user).returns purchase.user Purchase.any_instance.stubs(:refund).returns true do_destroy response.should redirect_to purchase.offer end it "does not refund the buyer when it fails" do subject.stubs(:current_user).returns purchase.user Purchase.any_instance.stubs(:refund).returns false put :destroy, :id => purchase.id response.should_not be_redirect end it "does not refund the buyer when it's the wrong user" do subject.stubs(:current_user).returns create_user Purchase.any_instance.expects(:refund).never do_destroy response.should_not be_redirect end end But then I heard the voice of an old friend in my head, saying (with a long trailing echo) "any_instance is terrible practice.. never use it!" So I am curious if anyone has suggestions on how this might be improved? Thank you. Patrick J. Collins http://collinatorstudios.com From zach.dennis at gmail.com Sat May 5 03:21:18 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Fri, 4 May 2012 23:21:18 -0400 Subject: [rspec-users] Preferred way to predict behavior in controller spec? In-Reply-To: References: Message-ID: On Fri, May 4, 2012 at 7:14 PM, Patrick J. Collins wrote: > So, I have an action like this: > > ?def destroy > ? ?if purchase.user == current_user && purchase.refund > ? ? ?redirect_to purchase > ? ?else > ? ? ?flash[:error] = t("purchases.refund.failure") > ? ? ?render :show > ? ?end > ?end > > ... ?I wanted a controller test to verify the following: > > ?1. ?It's the correct user > ?2. ?The refund was successful > ?3. ?User is redirected > > ... > > Initially, I thought this would go something like this: > > ?describe "#destroy" do > ? ?let(:purchase) { create_purchase } > > ? ?it "refunds the buyer" do > ? ? ?subject.stubs(:current_user).returns purchase.user > ? ? ?purchase.stubs(:refund).returns true > ? ? ?put :destroy, { :id => purchase.id } > ? ? ?response.should be_redirect > ? ?end > ?end > > WRONG!!!!!!!!! > > Obviously, that is not cool because purchase.stubs(:refund) will not be the > right object in the context of the controller...... ?So since my controller is actually doing the following: > > ?def destroy > ? ?if purchase.user # etc... > ?end > > ?private > > ?def purchase > ? ?@purchase ||= Purchase.find(params[:id]) > ?end > > I could do: > > subject.stubs(:purchase).returns(purchase) > > .. ?However, now that kind of defeats the purpose of put :destroy, { :id => purchase.id }... > > So I didn't really like that-- I am no longer verifying it's the right record, > so maybe the things I wanted to test were actually: > > ?1. ?It finds the purchase > ?2. ?It's the correct user > ?3. ?The refund was successful > ?4. ?User is redirected > > ........ > > So I ended up making my test do: > > ?describe "#destroy" do > ? ?let(:purchase) { create_purchase } > > ? ?def do_destroy > ? ? ?put :destroy, { :id => purchase.id } > ? ?end > > ? ?it "refunds the buyer" do > ? ? ?subject.stubs(:current_user).returns purchase.user > ? ? ?Purchase.any_instance.stubs(:refund).returns true > ? ? ?do_destroy > ? ? ?response.should be_redirect > ? ?end > ? ? ?subject.stubs(:current_user).returns purchase.user > ? ? ?Purchase.any_instance.stubs(:refund).returns true > > ? ? ?do_destroy > ? ? ?response.should redirect_to purchase.offer > ? ?end > > ? ?it "does not refund the buyer when it fails" do > ? ? ?subject.stubs(:current_user).returns purchase.user > ? ? ?Purchase.any_instance.stubs(:refund).returns false > > ? ? ?put :destroy, :id => purchase.id > ? ? ?response.should_not be_redirect > ? ?end > > ? ?it "does not refund the buyer when it's the wrong user" do > ? ? ?subject.stubs(:current_user).returns create_user > ? ? ?Purchase.any_instance.expects(:refund).never > > ? ? ?do_destroy > ? ? ?response.should_not be_redirect > ? ?end > ?end > > But then I heard the voice of an old friend in my head, saying (with a long > trailing echo) "any_instance is terrible practice.. never use it!" > > So I am curious if anyone has suggestions on how this might be improved? Instead of any_instance you could stub the interaction with Purchase.find in your spec: Purchase.stubs(:find).with(purchase.id).returns purchase HTH, Zach > > Thank you. > > Patrick J. Collins > http://collinatorstudios.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com From dchelimsky at gmail.com Sat May 5 06:39:42 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 5 May 2012 01:39:42 -0500 Subject: [rspec-users] rspec-mocks and rspec-rails-2.10.1 are released! Message-ID: These are patch releases recommended for anybody who has already upgraded to 2.10. ### rspec-mocks-2.10.1 full changelog: http://github.com/rspec/rspec-mocks/compare/v2.10.0...v2.10.1 Bug fixes * fix [regression of edge case behavior](https://github.com/rspec/rspec-mocks/issues/132) * fixed failure of `object.should_receive(:message).at_least(0).times.and_return value` * fixed failure of `object.should_not_receive(:message).and_return value` ### rspec-rails-2.10.1 full changelog: http://github.com/rspec/rspec-rails/compare/v2.10.0...v2.10.1 Bug fixes * fix [regression introduced in 2.10.0 that broke integration with Devise](https://github.com/rspec/rspec-rails/issues/534) From matt at mattwynne.net Sat May 5 08:48:33 2012 From: matt at mattwynne.net (Matt Wynne) Date: Sat, 5 May 2012 09:48:33 +0100 Subject: [rspec-users] rspec-2.10 is released! In-Reply-To: <26F528B93AEE43C5AC094210994D2C35@gmail.com> References: <26F528B93AEE43C5AC094210994D2C35@gmail.com> Message-ID: Corrections to documentation links: On 4 May 2012, at 02:37, David Chelimsky wrote: > rspec-2.10 is released! > > Cucumber docs > > http://rubydoc.info/gems/rspec-core > http://rubydoc.info/gems/rspec-expectations > http://rubydoc.info/gems/rspec-mocks > http://rubydoc.info/gems/rspec-rails http://relishapp.com/rspec/rspec-core http://relishapp.com/rspec/rspec-expectations http://relishapp.com/rspec/rspec-mocks http://relishapp.com/rspec/rspec-rails > > API Docs (RDoc) > > http://relishapp.com/gems/rspec-core > http://relishapp.com/gems/rspec-expectations > http://relishapp.com/gems/rspec-mocks > http://relishapp.com/gems/rspec-rails http://rubydoc.info/gems/rspec-core http://rubydoc.info/gems/rspec-expectations http://rubydoc.info/gems/rspec-mocks http://rubydoc.info/gems/rspec-rails > ### rspec-core-2.10.0 > > full changelog: http://github.com/rspec/rspec-core/compare/v2.9.0...v2.10.0 > > Enhancements > > * Add `prepend_before` and `append_after` hooks (preethiramdev) > * intended for extension libs > * restores rspec-1 behavior > * Reporting of profiled examples (moro) > * Report the total amount of time taken for the top slowest examples. > * Report what percentage the slowest examples took from the total runtime. > > Bug fixes > > * Properly parse `SPEC_OPTS` options. > * `example.description` returns the location of the example if there is no > explicit description or matcher-generated description. > * RDoc fixes (Grzegorz ?wirski) > * Do not modify example ancestry when dumping errors (Michael Grosser) > > ### rspec-expectations-2.10.0 > > full changelog: http://github.com/rspec/rspec-expectations/compare/v2.9.1...v2.10.0 > > Enhancements > > * Add new `start_with` and `end_with` matchers (Jeremy Wadsack) > * Add new matchers for specifying yields (Myron Marson): > * `expect {...}.to yield_control` > * `expect {...}.to yield_with_args(1, 2, 3)` > * `expect {...}.to yield_with_no_args` > * `expect {...}.to yield_successive_args(1, 2, 3)` > * `match_unless_raises` takes multiple exception args > > Bug fixes > > * Fix `be_within` matcher to be inclusive of delta. > * Fix message-specific specs to pass on Rubinius (John Firebaugh) > > ### rspec-mocks-2.10.0 > > full changelog: http://github.com/rspec/rspec-mocks/compare/v2.9.0...v2.10.0 > > Bug fixes > > * fail fast when an `exactly` or `at_most` expectation is exceeded > > ### rspec-rails-2.10.0 > > full changelog: http://github.com/rspec/rspec-core/compare/v2.9.0...v2.10.0 > > Bug fixes > > * `render_views` called in a spec can now override the config setting. (martinsvalin) > * Fix `render_views` for anonymous controllers on 1.8.7. (hudge, mudge) > * Eliminate use of deprecated `process_view_paths` > * Fix false negatives when using `route_to` matcher with `should_not` > * `controller` is no longer nil in `config.before` hooks > * Change `request.path_parameters` keys to symbols to match real Rails > environment (Nathan Broadbent) > * Silence deprecation warnings in pre-2.9 generated view specs > (Jonathan del Strother) > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Maybe you need some tests for this email template ;) cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From matt.spendlove at gmail.com Tue May 8 14:43:29 2012 From: matt.spendlove at gmail.com (msp) Date: Tue, 8 May 2012 07:43:29 -0700 (PDT) Subject: [rspec-users] should_receive_chain In-Reply-To: <60C7B512-E443-4CF7-AA57-6FAE045567AC@gmail.com> References: <31ad4dd7-9b87-4cb5-8bd3-de7237f229e0@qt10g2000pbb.googlegroups.com> <8312878.51.1329857292526.JavaMail.geo-discussion-forums@ynnj12> <60C7B512-E443-4CF7-AA57-6FAE045567AC@gmail.com> Message-ID: <7990800.29.1336488209297.JavaMail.geo-discussion-forums@ynjn12> Hi all On Wednesday, February 22, 2012 9:15:56 PM UTC, Justin Ko wrote: > > > On Feb 22, 2012, at 9:00 AM, Matt Wynne wrote: > > > On 22 Feb 2012, at 15:41, David Chelimsky wrote: > > On Tue, Feb 21, 2012 at 2:48 PM, Mike Pack wrote: > > Yup, I find myself doing this all the time. I think it should be considered > > that too deep of a stub chain could be a sign of poor > > abstraction/information hiding. Could lead to bad practices? On the > > flipside, this would be super helpful when dealing with Railsy stubs > because > > of long scope chains (but IMO long scope chains should be enclosed in a > > method). > > > Also, why not rspec core? > > > First - it would be rspec-mocks, not rspec-core. > > Second - in all but the rarest cases (mostly fluent interfaces), it > exacerbates highly coupled designs by making them seemingly easier to > test (but in the long run they just add to the problems associated w/ > coupling). This is already true of stub_chain, which I already regret > including in rspec-mocks for these reasons. If @justinko introduces a > separate gem for should_receive_chain, I'd probably want to move > stub_chain to that gem as well. > > Note that I'm not saying that every use of stub_chain is incorrect, or > un-pragmatic. I just think that if there's another way to get at that > feature, rspec-mocks is better off without it. > > > ^ Yep, what he said ^ > > cheers, > Matt > > -- > Freelance programmer & coach > Author, http://pragprog.com/book/hwcuc/the-cucumber-book > Founder, http://www.relishapp.com/ > Twitter, https://twitter.com/mattwynne > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > Now that I think about it, the only time I use stub_chain is in Rails > controllers, for scopes (which is also pretty rare). I'm 100% for > extracting stub_chain into a gem, along with any_instance. We could call it > "rspec-mocks-extensions" :) > Came to this thread as I'm curious about how folk test data collection in their controllers. I feel I just want to just test message expectations as simple controllers are pretty dumb right? I certainly don't wanna implement my own query interface (wrapping all scopes in methods) to enable setting message expectations and it seems we can stub (scope) chains but not set expectations. Just doesn't quite feel right all of this.. Are you all really doing the kinda thing Justin suggests at the start of this discussion? Best. -------------- next part -------------- An HTML attachment was scrubbed... URL: From samer.masry at gmail.com Tue May 8 17:55:02 2012 From: samer.masry at gmail.com (Samer Masry) Date: Tue, 8 May 2012 10:55:02 -0700 Subject: [rspec-users] Retrying specs Message-ID: Is it possible to run an example without displaying it's status ...F... I'm currently using an around(:each) to run the spec and passing the example block into vcr however occasionally the cassette needs to be rerecorded. I'd like to delay recording it as an error until it has been retried. Is that possible currently in rspec? -- DryBlis - www.dryblis.com Samer Masry | Rails Programmer 11 Ambler Ln, Emeryville, CA 94608 Tel.:510-991-7523 | Cel.: 714-814-8508 From lists at ruby-forum.com Tue May 8 18:15:06 2012 From: lists at ruby-forum.com (Roger Pack) Date: Tue, 08 May 2012 20:15:06 +0200 Subject: [rspec-users] rspec-mocks and rspec-rails-2.10.1 are released! In-Reply-To: References: Message-ID: <2d0bd2bbbb851329bde9f76274daa83d@ruby-forum.com> Thanks David. -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Wed May 9 05:00:45 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 9 May 2012 00:00:45 -0500 Subject: [rspec-users] Retrying specs In-Reply-To: References: Message-ID: On Tue, May 8, 2012 at 12:55 PM, Samer Masry wrote: > Is it possible to run an example without displaying it's > status ...F... The 'F' comes from the ProgressFormatter. You could write your own custom formatter. https://www.relishapp.com/rspec/rspec-core/docs/formatters/custom-formatters > I'm currently using an around(:each) to run the spec and passing the > example block into vcr however occasionally the cassette needs to be > rerecorded. ?I'd like to delay recording it as an error until it has been > retried. ?Is that possible currently in rspec? You could probably hack something together but rspec doesn't support any notion of retry explicitly. Feel free to submit a feature request to https://github.com/rspec/rspec-core/issues and we can discuss it there. Cheers, David > > -- > DryBlis - www.dryblis.com > Samer Masry | Rails Programmer > 11 Ambler Ln, Emeryville, CA 94608 > Tel.:510-991-7523 | Cel.: 714-814-8508 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From samer.masry at gmail.com Wed May 9 05:15:07 2012 From: samer.masry at gmail.com (Samer Masry) Date: Tue, 8 May 2012 22:15:07 -0700 Subject: [rspec-users] Retrying specs In-Reply-To: References: Message-ID: <0D21FCAD-109A-4179-B717-95F643D37C58@gmail.com> Forgot to respond back. I was able to reset the factory girl sequence by calling FactoryGirl.reload in a before callback and ditched the retry which solved my problem. Thanks -S On May 8, 2012, at 10:00 PM, David Chelimsky wrote: > On Tue, May 8, 2012 at 12:55 PM, Samer Masry wrote: >> Is it possible to run an example without displaying it's >> status ...F... > > The 'F' comes from the ProgressFormatter. You could write your own > custom formatter. > > https://www.relishapp.com/rspec/rspec-core/docs/formatters/custom-formatters > >> I'm currently using an around(:each) to run the spec and passing the >> example block into vcr however occasionally the cassette needs to be >> rerecorded. I'd like to delay recording it as an error until it has been >> retried. Is that possible currently in rspec? > > You could probably hack something together but rspec doesn't support > any notion of retry explicitly. Feel free to submit a feature request > to https://github.com/rspec/rspec-core/issues and we can discuss it > there. > > Cheers, > David > >> >> -- >> DryBlis - www.dryblis.com >> Samer Masry | Rails Programmer >> 11 Ambler Ln, Emeryville, CA 94608 >> Tel.:510-991-7523 | Cel.: 714-814-8508 >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From apremdas at gmail.com Wed May 9 16:01:18 2012 From: apremdas at gmail.com (Andrew Premdas) Date: Wed, 9 May 2012 17:01:18 +0100 Subject: [rspec-users] stub.as_null_object mock and double Message-ID: Hi there, Is there any particular reason why stub is not stub.as_null_object by default? Or alternatively is there some nicer sugar for stub.as_null_object Is RSpec trying to discourage the use of stub.as_null_object, and if so why? TIA Andrew -- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Wed May 9 16:30:20 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 9 May 2012 11:30:20 -0500 Subject: [rspec-users] stub.as_null_object mock and double In-Reply-To: References: Message-ID: On Wed, May 9, 2012 at 11:01 AM, Andrew Premdas wrote: > Hi there, > > Is there any particular reason why stub is not stub.as_null_object by > default? See https://github.com/rspec/rspec-mocks/issues/56 From patrick at collinatorstudios.com Wed May 9 20:09:04 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Wed, 9 May 2012 13:09:04 -0700 (PDT) Subject: [rspec-users] is there a way to turn on :js => true in the middle of a spec? Message-ID: Hi, If I have a spec with some complicated background stuff-- like say for example logging in... it "does stuff", :js => true do fancy_login_helper_method visit somewhere_over_the_rainbow_path click_button "omg" page.should have_content("waka waka") end ... Is there any way to do something like : it "does stuff" do fancy_login_helper_method js do visit somewhere_over_the_rainbow_path click_button "omg" page.should have_content("waka waka") end end So that I can cut down the time it takes to excute that test a bit? Or is that just impossible with the way headless vs browser stuff works? Patrick J. Collins http://collinatorstudios.com From dchelimsky at gmail.com Wed May 9 20:44:25 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 9 May 2012 15:44:25 -0500 Subject: [rspec-users] is there a way to turn on :js => true in the middle of a spec? In-Reply-To: References: Message-ID: On Wed, May 9, 2012 at 3:09 PM, Patrick J. Collins wrote: > Hi, > > If I have a spec with some complicated background stuff-- ?like say for > example logging in... > > it "does stuff", :js => true do > ?fancy_login_helper_method > ?visit somewhere_over_the_rainbow_path > ?click_button "omg" > ?page.should have_content("waka waka") > end > > ... ?Is there any way to do something like : > > it "does stuff" do > ?fancy_login_helper_method > ?js do > ? ?visit somewhere_over_the_rainbow_path > ? ?click_button "omg" > ? ?page.should have_content("waka waka") > ?end > end > > So that I can cut down the time it takes to excute that test a bit? ?Or > is that just impossible with the way headless vs browser stuff works? iiuc, browser is the issue, not headless. The login token/cookie/whatever needs to be stored in the client so it can identify itself to the server. What your proposing switches the client. From patrick at collinatorstudios.com Thu May 10 04:14:38 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Wed, 9 May 2012 21:14:38 -0700 (PDT) Subject: [rspec-users] how do I test for a status code in a :js => true test? Message-ID: I have a view with some javascript that does page redirection, and I wanted to confirm that under certain circumstances the user will get a 500 error from CanCan::AccessDenied. It seems that when I am not using :js => true, I can do: page.status_code.shoud be 500 but.. when I try that in a :js => true example group, I get: Capybara::NotSupportedByDriverError: Capybara::NotSupportedByDriverError also the request and response objects are nil in the context of this test... My dirty workaround has just been to do: page.has_content "CanCan::AccessDenied" Which serves my purposes, but I was hoping for a better way... Any ideas? Thank you. Patrick J. Collins http://collinatorstudios.com From dchelimsky at gmail.com Thu May 10 11:45:23 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 10 May 2012 06:45:23 -0500 Subject: [rspec-users] how do I test for a status code in a :js => true test? In-Reply-To: References: Message-ID: On Wed, May 9, 2012 at 11:14 PM, Patrick J. Collins wrote: > I have a view with some javascript that does page redirection, and I > wanted to confirm that under certain circumstances the user will get a > 500 error from CanCan::AccessDenied. > > It seems that when I am not using :js => true, I can do: > > page.status_code.shoud be 500 > > but.. when I try that in a :js => true example group, I get: > Capybara::NotSupportedByDriverError: Capybara::NotSupportedByDriverError > > also the request and response objects are nil in the context of this > test... ?My dirty workaround has just been to do: > > page.has_content "CanCan::AccessDenied" > > Which serves my purposes, but I was hoping for a better way... ?Any > ideas? Not sure. I'd post this to the Capybara list. From chavedomundo at gmail.com Fri May 11 14:24:19 2012 From: chavedomundo at gmail.com (Alexandre de Oliveira) Date: Fri, 11 May 2012 11:24:19 -0300 Subject: [rspec-users] Stubs and contract tests Message-ID: Hey guys, could you help me (in)validating an idea? As you might know, Contract tests solve a problem stubs have: if the object interface changes, the stubbed tests will continue passing. In RSpec, it's common to use a shared_context to describe contracts, so both Posts and Comments tests would have PostsCommentsContract. If Comment's interface ever changed, Posts tests would present a failure wherever we ran it. However, it won't tell us which stubbed methods were invalid now; it just tell us the file that have invalid stubs. If we had, for example, #stub_contract(:method), automatically raising a failure if its reference (double) object's interface changed, how would it be bad? I'm trying to think the cons of it. Could you guys help me finding it? --Alexandre -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Fri May 11 16:48:54 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 11 May 2012 11:48:54 -0500 Subject: [rspec-users] Stubs and contract tests In-Reply-To: References: Message-ID: On Fri, May 11, 2012 at 9:24 AM, Alexandre de Oliveira < chavedomundo at gmail.com> wrote: > Hey guys, could you help me (in)validating an idea? > > As you might know, Contract tests solve a problem stubs have: if the > object interface changes, the stubbed tests will continue passing. > > In RSpec, it's common to use a shared_context to describe contracts, so > both Posts and Comments tests would have PostsCommentsContract. If > Comment's interface ever changed, Posts tests would present a failure > wherever we ran it. However, it won't tell us which stubbed methods were > invalid now; it just tell us the file that have invalid stubs. > > If we had, for example, #stub_contract(:method), automatically raising > a failure if its reference (double) object's interface changed, how would > it be bad? > > I'm trying to think the cons of it. Could you guys help me finding it? > See https://github.com/rspec/rspec-mocks/issues/15 -------------- next part -------------- An HTML attachment was scrubbed... URL: From sahmed1020 at gmail.com Mon May 14 19:23:31 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Mon, 14 May 2012 15:23:31 -0400 Subject: [rspec-users] overview of how rspec was designed Message-ID: Other than jumping into the codebase myself, I was wondering if anyone has done a high-level (or better yet low level) write-up on how rspec works under the covers? -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick at collinatorstudios.com Tue May 15 06:47:58 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Mon, 14 May 2012 23:47:58 -0700 (PDT) Subject: [rspec-users] how can I do an "or" within the context of a matcher? Message-ID: Capybara has two methods: page.has_button? and page.has_link? ... I am wondering, how can I make a helper method that will be satisfied by one or the other? In other words, I want to be able to do: page.should have_link_or_button("blah") def have_link_or_button(locator) have_link(locator) || have_button(locator) end But that doesn't work... Is there any way to do this? Patrick J. Collins http://collinatorstudios.com From peter.fitzgibbons at gmail.com Tue May 15 11:32:05 2012 From: peter.fitzgibbons at gmail.com (Peter Fitzgibbons) Date: Tue, 15 May 2012 06:32:05 -0500 Subject: [rspec-users] Bash shim for Rspec Message-ID: HI Folks, The following script is *supposed* to run rspec under Jruby with the proper Java-switch for using SWT with OSX (-XstartOnFirstThread). The issue is that this script doesn't actually run any of the specs! Example: $ bin/rspec_auto_jvm spec/swt_shoes/app_spec.rb spec/swt_shoes/app_spec.rb args are spec/swt_shoes/app_spec.rb $ Can anyone point out the flaw? Peter Fitzgibbons (847) 859-9550 Email: peter.fitzgibbons at gmail.com IM GTalk: peter.fitzgibbons IM AOL: peter.fitzgibbons at gmail.com #!/usr/bin/env bash # !/usr/bin/env jruby --1.9 --debug # # This file was generated by Bundler. # # The application 'rspec' is installed as part of a gem, and # this file is here to facilitate running it. # echo "$@" | grep 'swt' if [[ $@ == *swt* ]]; then swt_opt="-J-XstartOnFirstThread" fi echo 'args are ' $* run_rspec=" # http://jira.codehaus.org/browse/JRUBY-6324 # random seed for srand is not initialized properly # call once and throw-away nil default first value x = srand require 'rubygems' version = \">= 0\" if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then version = $1 ARGV.shift end gem 'rspec-core', version load Gem.bin_path('rspec-core', 'rspec', version) exit 0 " jruby --1.9 --debug ${swt_opt} -e ${run_rspec} $* -------------- next part -------------- An HTML attachment was scrubbed... URL: From lbocseg at yahoo.com.br Tue May 15 14:18:05 2012 From: lbocseg at yahoo.com.br (Rodrigo Rosenfeld Rosas) Date: Tue, 15 May 2012 11:18:05 -0300 Subject: [rspec-users] how can I do an "or" within the context of a matcher? In-Reply-To: References: Message-ID: <4FB2659D.9050106@yahoo.com.br> You're looking for Custom Matchers: https://github.com/dchelimsky/rspec/wiki/Custom-Matchers Em 15-05-2012 03:47, Patrick J. Collins escreveu: > Capybara has two methods: > > page.has_button? > > and > > page.has_link? > > ... I am wondering, how can I make a helper method that will be > satisfied by one or the other? In other words, I want to be able to do: > > page.should have_link_or_button("blah") > > def have_link_or_button(locator) > have_link(locator) || have_button(locator) > end > > But that doesn't work... Is there any way to do this? > > Patrick J. Collins > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Tue May 15 16:03:52 2012 From: jko170 at gmail.com (Justin Ko) Date: Tue, 15 May 2012 10:03:52 -0600 Subject: [rspec-users] how can I do an "or" within the context of a matcher? In-Reply-To: References: Message-ID: <1579E24B-0622-4F9D-8D73-39146554CAE5@gmail.com> On May 15, 2012, at 12:47 AM, Patrick J. Collins wrote: > Capybara has two methods: > > page.has_button? > > and > > page.has_link? > > ... I am wondering, how can I make a helper method that will be > satisfied by one or the other? In other words, I want to be able to do: > > page.should have_link_or_button("blah") > > def have_link_or_button(locator) > have_link(locator) || have_button(locator) > end > > But that doesn't work... Is there any way to do this? > > Patrick J. Collins > http://collinatorstudios.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users You need a custom matcher, something like this: RSpec::Matchers.define :have_link_or_button do |locator| match do |page| page.has_button?(locator) || page.has_link?(locator) end end BTW, I believe Capybara has a method that checks for a button *or* link - look into that. From dchelimsky at gmail.com Tue May 15 17:41:24 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 15 May 2012 12:41:24 -0500 Subject: [rspec-users] overview of how rspec was designed In-Reply-To: References: Message-ID: On Mon, May 14, 2012 at 2:23 PM, S Ahmed wrote: > Other than jumping into the codebase myself, I was wondering if anyone has > done a high-level (or better yet low level) write-up on how rspec works > under the covers? The RSpec Book has a fairly deep discussion on the inner workings. Other than that, there are some things you can get from the docs, but there is not a central "this is how RSpec works" document that I've written or am aware of. From peter.fitzgibbons at gmail.com Wed May 16 09:56:53 2012 From: peter.fitzgibbons at gmail.com (Peter Fitzgibbons) Date: Wed, 16 May 2012 04:56:53 -0500 Subject: [rspec-users] Bash shim for Rspec In-Reply-To: References: Message-ID: HI Folks, The following script is *supposed* to run rspec under Jruby with the proper Java-switch for using SWT with OSX (-XstartOnFirstThread). The issue is that this script doesn't actually run any of the specs! Example: $ bin/rspec_auto_jvm spec/swt_shoes/app_spec.rb spec/swt_shoes/app_spec.rb args are spec/swt_shoes/app_spec.rb $ Can anyone point out the flaw? Peter Fitzgibbons (847) 859-9550 Email: peter.fitzgibbons at gmail.com IM GTalk: peter.fitzgibbons IM AOL: peter.fitzgibbons at gmail.com #!/usr/bin/env bash # !/usr/bin/env jruby --1.9 --debug # # This file was generated by Bundler. # # The application 'rspec' is installed as part of a gem, and # this file is here to facilitate running it. # echo "$@" | grep 'swt' if [[ $@ == *swt* ]]; then swt_opt="-J-XstartOnFirstThread" fi echo 'args are ' $* run_rspec=" # http://jira.codehaus.org/browse/JRUBY-6324 # random seed for srand is not initialized properly # call once and throw-away nil default first value x = srand require 'rubygems' version = \">= 0\" if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then version = $1 ARGV.shift end gem 'rspec-core', version load Gem.bin_path('rspec-core', 'rspec', version) exit 0 " jruby --1.9 --debug ${swt_opt} -e ${run_rspec} $* -------------- next part -------------- An HTML attachment was scrubbed... URL: From lbocseg at yahoo.com.br Fri May 18 00:04:23 2012 From: lbocseg at yahoo.com.br (Rodrigo Rosenfeld Rosas) Date: Thu, 17 May 2012 21:04:23 -0300 Subject: [rspec-users] How does Spork help in requests specs? Message-ID: <4FB59207.2020102@yahoo.com.br> Even with Spork, my requests specs are very slow to start running (about 7 seconds). I suspect Rails is booting each time I run "rspec -X spec/requests". Is that true? If so, is there any way I could instruct the web server to keep alive after the specs run so that it would be faster on next run? Are there any resources on how to have better performance on running requests specs with Capybara and Webkit? Having to wait about 7 seconds between consecutive runs is really a blocker for me. If I can't get it to run faster, I'll give up on requests specs and only have the controller's tests and client-side code specs without any integration tests... But to be honest, I feel much more comfortable having some integration tests for the main features of my application and I would appreciate any hints on making them start faster. The last time I worked with Rails, long ago, it wasn't such a painful experience to write such kind of tests (Capybara + Webkit). And my current desktop PC is *way* faster than it used to be by that time... I understand that Rails is much slower to boot now, but maybe there are some tricks for reducing the need of rebooting between consecutive requests specs run. Thanks in advance, Rodrigo. From lbocseg at yahoo.com.br Fri May 18 00:09:24 2012 From: lbocseg at yahoo.com.br (Rodrigo Rosenfeld Rosas) Date: Thu, 17 May 2012 21:09:24 -0300 Subject: [rspec-users] How does Spork help in requests specs? In-Reply-To: <4FB59207.2020102@yahoo.com.br> References: <4FB59207.2020102@yahoo.com.br> Message-ID: <4FB59334.3090101@yahoo.com.br> Sorry about this, I'm further investigating the slowness issue, and I found the culprit: truncation of my PostgreSQL are very slow. I'll investigate how to make it faster. Maybe deletion could be faster than truncation on PG... Cheers, Rodrigo. Em 17-05-2012 21:04, Rodrigo Rosenfeld Rosas escreveu: > Even with Spork, my requests specs are very slow to start running > (about 7 seconds). > > I suspect Rails is booting each time I run "rspec -X spec/requests". > > Is that true? If so, is there any way I could instruct the web server > to keep alive after the specs run so that it would be faster on next run? > > Are there any resources on how to have better performance on running > requests specs with Capybara and Webkit? > > Having to wait about 7 seconds between consecutive runs is really a > blocker for me. If I can't get it to run faster, I'll give up on > requests specs and only have the controller's tests and client-side > code specs without any integration tests... > > But to be honest, I feel much more comfortable having some integration > tests for the main features of my application and I would appreciate > any hints on making them start faster. > > The last time I worked with Rails, long ago, it wasn't such a painful > experience to write such kind of tests (Capybara + Webkit). And my > current desktop PC is *way* faster than it used to be by that time... > > I understand that Rails is much slower to boot now, but maybe there > are some tricks for reducing the need of rebooting between consecutive > requests specs run. > > Thanks in advance, > Rodrigo. > From lbocseg at yahoo.com.br Fri May 18 00:45:24 2012 From: lbocseg at yahoo.com.br (Rodrigo Rosenfeld Rosas) Date: Thu, 17 May 2012 21:45:24 -0300 Subject: [rspec-users] How does Spork help in requests specs? In-Reply-To: <4FB59334.3090101@yahoo.com.br> References: <4FB59207.2020102@yahoo.com.br> <4FB59334.3090101@yahoo.com.br> Message-ID: <4FB59BA4.4090806@yahoo.com.br> Indeed, deleting the tables manually requires more effort because of the foreign keys that will require a specific order, but the specs got way faster than using truncation. Go figure it out. And worse than that is reading this on PG documentation: http://www.postgresql.org/docs/9.1/static/sql-truncate.html "TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but*since it does not actually scan the tables it is faster*." Yeah, sure, I believed on you once. Maybe this is true for large tables, but not so true for testing matters. I just found that I should report this here for benefits of others that might consider this approach as well on PostgreSQL. Best, Rodrigo. Em 17-05-2012 21:09, Rodrigo Rosenfeld Rosas escreveu: > Sorry about this, I'm further investigating the slowness issue, and I > found the culprit: truncation of my PostgreSQL are very slow. I'll > investigate how to make it faster. Maybe deletion could be faster than > truncation on PG... > > Cheers, > Rodrigo. > > Em 17-05-2012 21:04, Rodrigo Rosenfeld Rosas escreveu: >> Even with Spork, my requests specs are very slow to start running >> (about 7 seconds). >> >> I suspect Rails is booting each time I run "rspec -X spec/requests". >> >> Is that true? If so, is there any way I could instruct the web server >> to keep alive after the specs run so that it would be faster on next >> run? >> >> Are there any resources on how to have better performance on running >> requests specs with Capybara and Webkit? >> >> Having to wait about 7 seconds between consecutive runs is really a >> blocker for me. If I can't get it to run faster, I'll give up on >> requests specs and only have the controller's tests and client-side >> code specs without any integration tests... >> >> But to be honest, I feel much more comfortable having some >> integration tests for the main features of my application and I would >> appreciate any hints on making them start faster. >> >> The last time I worked with Rails, long ago, it wasn't such a painful >> experience to write such kind of tests (Capybara + Webkit). And my >> current desktop PC is *way* faster than it used to be by that time... >> >> I understand that Rails is much slower to boot now, but maybe there >> are some tricks for reducing the need of rebooting between >> consecutive requests specs run. >> >> Thanks in advance, >> Rodrigo. -------------- next part -------------- An HTML attachment was scrubbed... URL: From toaksie at gmail.com Sat May 19 17:46:39 2012 From: toaksie at gmail.com (survivorist) Date: Sat, 19 May 2012 10:46:39 -0700 (PDT) Subject: [rspec-users] bug: rspec rails generator generates a failing spec In-Reply-To: <14403969.98.1332451445249.JavaMail.geo-discussion-forums@pbboc6> References: <16351794.73.1332451144960.JavaMail.geo-discussion-forums@pbbpk10> <14403969.98.1332451445249.JavaMail.geo-discussion-forums@pbboc6> Message-ID: <1337449599202-4975461.post@n6.nabble.com> What was the specific issue with this and how did you resolve it, I'm having the exact same error and can't find the root of the issue? Thanks -- View this message in context: http://ruby.11.n6.nabble.com/bug-rspec-rails-generator-generates-a-failing-spec-tp4647101p4975461.html Sent from the rspec-users mailing list archive at Nabble.com. From jko170 at gmail.com Sat May 19 20:09:20 2012 From: jko170 at gmail.com (Justin Ko) Date: Sat, 19 May 2012 14:09:20 -0600 Subject: [rspec-users] bug: rspec rails generator generates a failing spec In-Reply-To: <1337449599202-4975461.post@n6.nabble.com> References: <16351794.73.1332451144960.JavaMail.geo-discussion-forums@pbbpk10> <14403969.98.1332451445249.JavaMail.geo-discussion-forums@pbboc6> <1337449599202-4975461.post@n6.nabble.com> Message-ID: On May 19, 2012, at 11:46 AM, survivorist wrote: > What was the specific issue with this and how did you resolve it, I'm having > the exact same error and can't find the root of the issue? Thanks > > -- > View this message in context: http://ruby.11.n6.nabble.com/bug-rspec-rails-generator-generates-a-failing-spec-tp4647101p4975461.html > Sent from the rspec-users mailing list archive at Nabble.com. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users I believe this is fixed in RSpec 2.10 From lists at ruby-forum.com Wed May 23 22:27:45 2012 From: lists at ruby-forum.com (Kleber Shimabuku) Date: Thu, 24 May 2012 00:27:45 +0200 Subject: [rspec-users] received unexpected message :id= with (20) Message-ID: <5bd1154622ac6c5becfb55c48f0547ef@ruby-forum.com> Please help me to understand this error message: 1) PostsController#create when logged in should create a new post and redirect to a successful message page Failure/Error: post 'create', { post: {title: 'New post', Mock Post(id: integer, title: string, description: text, location: string, created_at: datetime, updated_at: datetime, status: string, tags: string, views: integer, user_id: integer) received unexpected message :id= with (20) # ./spec/controllers/posts_controller_spec.rb:56:in `block (4 levels) in ' Finished in 0.59991 seconds 6 examples, 1 failure I'm trying to figure out this for about a week or more :( Attachments: http://www.ruby-forum.com/attachment/7429/posts_controller_spec.rb http://www.ruby-forum.com/attachment/7430/posts_controller.rb -- Posted via http://www.ruby-forum.com/. From patrick at collinatorstudios.com Thu May 24 00:12:54 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Wed, 23 May 2012 17:12:54 -0700 (PDT) Subject: [rspec-users] shared examples with a block for expects? Message-ID: Hi, Basically I want to do something like this: describe FancyThingsController do def do_new get :new end def do_create post :create, :fancy_thing => { :some => "params" } end shared_examples "an access controlled action" do |expectation| it "denies access" do expect(&expectation).to raise_error(CanCan::AccessDenied) end end describe "#new" do it_behaves_like "an access controlled action", proc { do_new } end describe "#create" do it_behaves_like "an access controlled action", proc { do_create } end end but I get: "expected CanCan::AccessDenied, got #" Just curious, how can I do this? Patrick J. Collins http://collinatorstudios.com From jko170 at gmail.com Thu May 24 06:28:26 2012 From: jko170 at gmail.com (Justin Ko) Date: Thu, 24 May 2012 00:28:26 -0600 Subject: [rspec-users] received unexpected message :id= with (20) In-Reply-To: <5bd1154622ac6c5becfb55c48f0547ef@ruby-forum.com> References: <5bd1154622ac6c5becfb55c48f0547ef@ruby-forum.com> Message-ID: <59E86DA9-E912-430F-AEBD-89D05021ED4E@gmail.com> On May 23, 2012, at 4:27 PM, Kleber Shimabuku wrote: > Please help me to understand this error message: > > 1) PostsController#create when logged in should create a new post and > redirect to a successful message page > Failure/Error: post 'create', { post: {title: 'New post', > Mock Post(id: integer, title: string, description: text, > location: string, created_at: datetime, updated_at: datetime, status: > string, tags: string, views: integer, user_id: integer) received > unexpected message :id= with (20) I'm guessing the `Post` mock is making its way to the model layer. Either way, you need to find out why `id=` is getting called. Shot in the dark: There is a lot of "post" names there. Maybe there is a conflict? > # ./spec/controllers/posts_controller_spec.rb:56:in `block (4 > levels) in ' > > Finished in 0.59991 seconds > 6 examples, 1 failure > > > > I'm trying to figure out this for about a week or more :( > > Attachments: > http://www.ruby-forum.com/attachment/7429/posts_controller_spec.rb > http://www.ruby-forum.com/attachment/7430/posts_controller.rb > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From jko170 at gmail.com Thu May 24 06:33:39 2012 From: jko170 at gmail.com (Justin Ko) Date: Thu, 24 May 2012 00:33:39 -0600 Subject: [rspec-users] shared examples with a block for expects? In-Reply-To: References: Message-ID: On May 23, 2012, at 6:12 PM, Patrick J. Collins wrote: > Hi, > > Basically I want to do something like this: > > describe FancyThingsController do > > def do_new > get :new > end > > def do_create > post :create, :fancy_thing => { :some => "params" } > end > > shared_examples "an access controlled action" do |expectation| > it "denies access" do > expect(&expectation).to raise_error(CanCan::AccessDenied) > end > end > > describe "#new" do > it_behaves_like "an access controlled action", proc { do_new } > end > > describe "#create" do > it_behaves_like "an access controlled action", proc { do_create } > end > Just use a Proc: describe '#new' do it_behaves_like 'an access controlled action', -> { get :new } end > end > > but I get: "expected CanCan::AccessDenied, got # local variable or method `do_create' for > RSpec::Core::ExampleGroup::Nested_1::Nested_2:Class>" > > Just curious, how can I do this? > > Patrick J. Collins > http://collinatorstudios.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From lists at ruby-forum.com Tue May 29 15:49:15 2012 From: lists at ruby-forum.com (=?UTF-8?B?RnLDqWTDqXJpYw==?= Champreux) Date: Tue, 29 May 2012 17:49:15 +0200 Subject: [rspec-users] RSpec fails testing on ActiveRecord Message-ID: <3e7ae9faa8b6a7674665e3b7daf8f2df@ruby-forum.com> Hi, I'm new to RoR, and following the RoR 3.2 Tutorial from Michael Hartl. When it comes to execute testing (chap. 3.2.1), RSpec returns a hundred of errors starting with this one (and all looking the same more or less): /home/fred/.rvm/gems/ruby-1.9.3-p0 at ODQ/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified) My DEV database is PostgreSQL and looks to work fine. Can some help me to understand what's wrong and solve it ? Thanks. Attachments: http://www.ruby-forum.com/attachment/7447/Gemfile http://www.ruby-forum.com/attachment/7448/database.yml http://www.ruby-forum.com/attachment/7449/Ruby_on_Rails__Welcome_aboard.html -- Posted via http://www.ruby-forum.com/. From kashyap.kmbc at gmail.com Tue May 29 15:54:51 2012 From: kashyap.kmbc at gmail.com (Kashyap KMBC) Date: Tue, 29 May 2012 21:24:51 +0530 Subject: [rspec-users] RSpec fails testing on ActiveRecord In-Reply-To: <3e7ae9faa8b6a7674665e3b7daf8f2df@ruby-forum.com> References: <3e7ae9faa8b6a7674665e3b7daf8f2df@ruby-forum.com> Message-ID: Is there a 'database.yml' config file in your app? It should be under "/config" folder. In that, you need to specify the adapter. As per the logs, looks like the adapter is not specified or else the config file is not loaded into the app in runtime. -- Kashyap KMBC On Tuesday 29 May 2012 at 9:19 PM, Fr?d?ric Champreux wrote: > Hi, > > I'm new to RoR, and following the RoR 3.2 Tutorial from Michael Hartl. > > When it comes to execute testing (chap. 3.2.1), RSpec returns a hundred > of errors starting with this one (and all looking the same more or > less): > > /home/fred/.rvm/gems/ruby-1.9.3-p0 at ODQ/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in > `resolve_hash_connection': database configuration does not specify > adapter (ActiveRecord::AdapterNotSpecified) > > My DEV database is PostgreSQL and looks to work fine. > > Can some help me to understand what's wrong and solve it ? > > Thanks. > > Attachments: > http://www.ruby-forum.com/attachment/7447/Gemfile > http://www.ruby-forum.com/attachment/7448/database.yml > http://www.ruby-forum.com/attachment/7449/Ruby_on_Rails__Welcome_aboard.html > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org (mailto:rspec-users at rubyforge.org) > http://rubyforge.org/mailman/listinfo/rspec-users > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From todd.sedano at sv.cmu.edu Tue May 29 15:59:36 2012 From: todd.sedano at sv.cmu.edu (Todd Sedano) Date: Tue, 29 May 2012 08:59:36 -0700 Subject: [rspec-users] RSpec fails testing on ActiveRecord In-Reply-To: <3e7ae9faa8b6a7674665e3b7daf8f2df@ruby-forum.com> References: <3e7ae9faa8b6a7674665e3b7daf8f2df@ruby-forum.com> Message-ID: You'll need to create a test database in postgres, and then modify your database.yml file to reference it. # PostgreSQL 8.4 development: adapter: postgresql encoding: unicode database: ODQ_APP pool: 5 test: adapter: postgresql encoding: unicode database: ODQ_APP_TEST pool: 5 Todd Sedano Director of Software Engineering Carnegie Mellon University Silicon Valley Campus Developing Software Leaders (TM) T: 650-335-2812 On Tue, May 29, 2012 at 8:49 AM, Fr?d?ric Champreux wrote: > Hi, > > I'm new to RoR, and following the RoR 3.2 Tutorial from Michael Hartl. > > When it comes to execute testing (chap. 3.2.1), RSpec returns a hundred > of errors starting with this one (and all looking the same more or > less): > > /home/fred/.rvm/gems/ruby-1.9.3-p0 at ODQ > /gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in > `resolve_hash_connection': database configuration does not specify > adapter (ActiveRecord::AdapterNotSpecified) > > My DEV database is PostgreSQL and looks to work fine. > > Can some help me to understand what's wrong and solve it ? > > Thanks. > > Attachments: > http://www.ruby-forum.com/attachment/7447/Gemfile > http://www.ruby-forum.com/attachment/7448/database.yml > > http://www.ruby-forum.com/attachment/7449/Ruby_on_Rails__Welcome_aboard.html > > > -- > 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 enderyurt at gmail.com Wed May 30 09:18:49 2012 From: enderyurt at gmail.com (zombiebit) Date: Wed, 30 May 2012 02:18:49 -0700 (PDT) Subject: [rspec-users] RSpec testing update action with ajax Message-ID: <1338369529332-4975810.post@n6.nabble.com> Hey, I want to test my Rails app. When you click the image ajax sends to data to DB and the data is updated in there. When I try to test, I faced with this problem, 1) RwsController Update widget score takes widget rate information Failure/Error: mock_widget.should_receive(:update_attributes).with({'these' => 'params'}) (Mock "Widget_1003").update_attributes({"these"=>"params"}) expected: 1 time received: 0 times # ./spec/controllers/rws_controller_spec.rb:28:in `block (3 levels) in ' *This is my actual controller which I try to test,* class RwsController < ApplicationController layout 'widget' skip_before_filter :verify_authenticity_token, :only => [:edit, :update, :show, :getUserInfo] respond_to :js, :json, :html #cookie check and show exact view def show @widget = Widget.find_by_uuid(params[:uuid]) if cookies["last_widget_id"] == @widget.uuid render :action => "generate" end @widget = Widget.find_by_uuid(params[:uuid]) end def edit @widget = Widget.find_by_uuid(params[:uuid]) end #after rating the rates and other details record on DB def update @widget = Widget.find_by_uuid(params[:uuid]) #logger.info("score-in: " + params[:score]) @widget.score = params[:score].to_i @widget.total_score = params[:total_score].to_i @widget.click_number = params[:click_number].to_i @widget.average = params[:average].to_i #set cookie cookies[:last_widget_id] = {:value => @widget.uuid, :expires => 1.year.from_now} @widget.save! #render :text => 'ok' #logger.info("score-out: " + @widget.score.to_s) #logger.info("cookie-id: " + cookies[:last_widget_id]) end #iframe creates and calls .js.erb file def generate @widget = Widget.find_by_uuid(params[:uuid]) #@widget_id = @widget.id respond_to do |format| format.js {} end end #recording the visitor who visit the page def getUserInfo data = params[:mainURL] data1 = params[:mainBrowserAgent] data2 = params[:mainReferer] data3 = params[:mainDisplayInfo] data4 = params[:currentWidgetId] @widgetTraffic = WidgetTraffic.new(params[:widget_traffic]) @widgetTraffic.widget_id = @widget_id @widgetTraffic.main_url = data @widgetTraffic.main_browser = data1 @widgetTraffic.main_referer = data2 @widgetTraffic.main_display = data3 @widgetTraffic.widget_id = data4 @widgetTraffic.save! render :text => 'ok' end end *And this is my test controller, * require 'spec_helper' describe RwsController do def mock_widget(stubs={}) @mock_widget ||= mock_model(Widget, stubs).as_null_object end describe "Get widget" do it "gets generated widget in iframe" do Widget.stub(:find_by_uuid).with("10") { mock_widget(:save => true) } get :generate ,:uuid => "10" assigns(:widget) == @widget response.should render_template(:js => "generate") end it "can not get generated widget in iframe" do Widget.stub(:find_by_uuid).with("10") { mock_widget(:save => false) } get :generate, :uuid => "10" assigns(:widget) == @widget response.code.should == "406" end end describe "Update widget score" do it "takes widget rate information" do Widget.stub(:find_by_uuid).with("6").and_return(mock_widget(:update_attributes => true)) mock_widget.should_receive(:update_attributes).with({'these' => 'params'}) xhr :put, :update, :uuid => "6", :widget => {'these' => 'params'}, :format => :js response.should be_success end end end *Where is the stuck point? How should I solve it?* -- View this message in context: http://ruby.11.n6.nabble.com/RSpec-testing-update-action-with-ajax-tp4975810.html Sent from the rspec-users mailing list archive at Nabble.com. From jko170 at gmail.com Wed May 30 18:42:24 2012 From: jko170 at gmail.com (Justin Ko) Date: Wed, 30 May 2012 12:42:24 -0600 Subject: [rspec-users] RSpec testing update action with ajax In-Reply-To: <1338369529332-4975810.post@n6.nabble.com> References: <1338369529332-4975810.post@n6.nabble.com> Message-ID: On May 30, 2012, at 3:18 AM, zombiebit wrote: > Hey, > I want to test my Rails app. When you click the image ajax sends to data to > DB and the data is updated in there. When I try to test, I faced with this > problem, > > > 1) RwsController Update widget score takes widget rate information > Failure/Error: > mock_widget.should_receive(:update_attributes).with({'these' => 'params'}) > (Mock "Widget_1003").update_attributes({"these"=>"params"}) > expected: 1 time > received: 0 times > # ./spec/controllers/rws_controller_spec.rb:28:in `block (3 levels) in > ' > > > *This is my actual controller which I try to test,* > > > class RwsController < ApplicationController > layout 'widget' > skip_before_filter :verify_authenticity_token, :only => [:edit, :update, > :show, :getUserInfo] > respond_to :js, :json, :html > > #cookie check and show exact view > def show > @widget = Widget.find_by_uuid(params[:uuid]) > if cookies["last_widget_id"] == @widget.uuid > render :action => "generate" > end > @widget = Widget.find_by_uuid(params[:uuid]) > end > > def edit > @widget = Widget.find_by_uuid(params[:uuid]) > end > > #after rating the rates and other details record on DB > def update > @widget = Widget.find_by_uuid(params[:uuid]) > #logger.info("score-in: " + params[:score]) > @widget.score = params[:score].to_i > @widget.total_score = params[:total_score].to_i > @widget.click_number = params[:click_number].to_i > @widget.average = params[:average].to_i > #set cookie > cookies[:last_widget_id] = {:value => @widget.uuid, :expires => > 1.year.from_now} > @widget.save! This is not #update_attributes > #render :text => 'ok' > #logger.info("score-out: " + @widget.score.to_s) > #logger.info("cookie-id: " + cookies[:last_widget_id]) > end > > #iframe creates and calls .js.erb file > def generate > @widget = Widget.find_by_uuid(params[:uuid]) > #@widget_id = @widget.id > respond_to do |format| > format.js {} > end > end > > #recording the visitor who visit the page > def getUserInfo > data = params[:mainURL] > data1 = params[:mainBrowserAgent] > data2 = params[:mainReferer] > data3 = params[:mainDisplayInfo] > data4 = params[:currentWidgetId] > @widgetTraffic = WidgetTraffic.new(params[:widget_traffic]) > @widgetTraffic.widget_id = @widget_id > @widgetTraffic.main_url = data > @widgetTraffic.main_browser = data1 > @widgetTraffic.main_referer = data2 > @widgetTraffic.main_display = data3 > @widgetTraffic.widget_id = data4 > > > @widgetTraffic.save! > render :text => 'ok' > end > end > > > *And this is my test controller, > * > require 'spec_helper' > > describe RwsController do > > def mock_widget(stubs={}) > @mock_widget ||= mock_model(Widget, stubs).as_null_object > end > > describe "Get widget" do > it "gets generated widget in iframe" do > Widget.stub(:find_by_uuid).with("10") { mock_widget(:save => true) } > get :generate ,:uuid => "10" > assigns(:widget) == @widget > response.should render_template(:js => "generate") > end > > it "can not get generated widget in iframe" do > Widget.stub(:find_by_uuid).with("10") { mock_widget(:save => false) } > get :generate, :uuid => "10" > assigns(:widget) == @widget > response.code.should == "406" > end > end > > describe "Update widget score" do > it "takes widget rate information" do > > Widget.stub(:find_by_uuid).with("6").and_return(mock_widget(:update_attributes > => true)) Need to pass #save! as the stub.... > mock_widget.should_receive(:update_attributes).with({'these' => > 'params'}) > xhr :put, :update, :uuid => "6", :widget => {'these' => 'params'}, > :format => :js > response.should be_success > end > end > > end > > *Where is the stuck point? How should I solve it?* > > -- > View this message in context: http://ruby.11.n6.nabble.com/RSpec-testing-update-action-with-ajax-tp4975810.html > Sent from the rspec-users mailing list archive at Nabble.com. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From patrick at collinatorstudios.com Wed May 30 18:55:28 2012 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Wed, 30 May 2012 11:55:28 -0700 (PDT) Subject: [rspec-users] alternate way to stub out I18n translations? Message-ID: Hi, I've run into a bit of a snag... I have a method like this class Thing def bar I18n.t("waka.waka", { :fozzy => "bear" }) end end And I was trying to make a test that did: describe "#bar" do it "translates like a mo fo" do I18n.stubs(:t).with("waka.waka", { :fozzy => "bear" }).returns "hooray!" subject.bar.should == "hooray!" end end However running this test gives me: Mocha::ExpectationError: unexpected invocation: I18n.t(:user_subject, {:scope => [:devise, :mailer, :confirmation_instructions], :default => [:subject, 'Confirmation instructions']} ... Which is obviously because when I stub the t method, I am killing any previous behavior.......... So, I am quite confused what to do here. If I try to just be more generic and do: describe "#bar" do it "translates like a mo fo" do I18n.stubs(:t).returns "hooray!" subject.bar.should == "hooray!" end end Then I get a whole different weird error: Failure/Error: let(:notification) { new_notification(:notifiable => create_offer, :action => "made") } ArgumentError: wrong number of arguments (4 for 5) ... But I don't want to be generic like that anyway--- So I am wondering what can I do to solve this? Does any one know of a way to temporarily add an entry into my en.yml locale dynamically? In other words, so that instead of stubbing I could just do something like: I18n.t("waka")[:waka] = "hooray!" and then in code can still access I18n.t("waka.waka").. But that doesn't work, and I am not sure how to pull that off... Any help would be greatly appreciated. Patrick J. Collins http://collinatorstudios.com From katrina.owen at gmail.com Wed May 30 20:19:59 2012 From: katrina.owen at gmail.com (Katrina Owen) Date: Wed, 30 May 2012 22:19:59 +0200 Subject: [rspec-users] alternate way to stub out I18n translations? In-Reply-To: References: Message-ID: On Wed, May 30, 2012 at 8:55 PM, Patrick J. Collins wrote: > Does any one know of a way to temporarily add an entry into my en.yml > locale dynamically? I usually do something like this in the before filter en = { :my => { :stuff => 'things' } } I18n.backend.store_translations(:en, en) Cheers, Katrina From enderyurt at gmail.com Thu May 31 08:37:29 2012 From: enderyurt at gmail.com (zombiebit) Date: Thu, 31 May 2012 01:37:29 -0700 (PDT) Subject: [rspec-users] RSpec controller methods testing Message-ID: <1338453449690-4975830.post@n6.nabble.com> *I am testing my controller and I run coverage "rcov" to learn how much I covered the test. However I cannot test Ruby methods. For example, This is my controller,* class AuthenticationsController < ApplicationController def index @authentications = current_user.authentications if current_user end #create an account via omniauth def create omniauth = request.env["omniauth.auth"] authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) if authentication sign_in_and_redirect(:user, authentication.user) elsif current_user current_user.build_with_provider(omniauth) redirect_to authentications_url else user = User.new user.build_with_provider(omniauth) user.save!(:validate => false) sign_in_and_redirect(:user, user) end end #signout def destroy @authentication = current_user.authentications.find(params[:id]) @authentication.destroy flash[:notice] = "Successfully destroyed authentication." redirect_to authentications_url end end *And I wrote a controller_spec to test it.* require 'spec_helper' describe AuthenticationsController do before(:each) do @current_user = mock_model(User, :id => 1) @authentications = mock_model(Authentication, :user_id => 1) Authentication.stub!(:current_user).and_return(@current_user) Authentication.stub!(:authentication).and_return(@authentications) end describe "Authentication" do it "should not current user if a current user does not exist" do subject.current_user.should_not be_nil redirect_to widgets_path end it "should not have a current_user" do redirect_to root_path end end def mock_auth(stubs={}) @mock_auth ||= mock_model(Authentication, stubs).as_null_object end describe "Get authentications" do it "should get all authentications " do Authentication.stub(:all) { [mock_auth] } get :index assigns(:authentication) == @authentications end end describe "Post authentication" do it "find an authentication" do Authentication.stub(:find_by_provider_and_uid).with("twitter", "12345").and_return(mock_auth) end it "authentication exists go user page" do end end end *I don not know how I can handle these methods. Do you have any idea? * -- View this message in context: http://ruby.11.n6.nabble.com/RSpec-controller-methods-testing-tp4975830.html Sent from the rspec-users mailing list archive at Nabble.com.