Thanks. I hadn't used class_double so I didn't know it needed
.as_stubbed_const to work in my context.

On Sun, Sep 7, 2014 at 4:38 PM, Jon Rowe <[email protected]> wrote:

> Hi Cynthia
>
> class_spy doesn’t override the constant automatically, it requires you to
> add .as_stubbed_const to override the constant so you call the spy from
> your controller.
>
> As you’ve observed class_spy is essentially class_double.as_null_object,
> see the docs here:
> https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/verifying-doubles/using-a-class-double
>
> Regards
> Jon Rowe
> ---------------------------
> jonrowe.co.uk
>
> On Monday, 8 September 2014 at 9:31, Cynthia Kiser wrote:
>
> I would like to test that a Rails controller I am writing deletes all
> NewsRelease objects and recreates them from an RSS feed. The new
> class_spy feature seemed like a good way to test that my controller action
> is calling the expected methods. But I can't seem to make it work - in
> context or even when I try to call the methods directly in the example (see
> final example below). I also tried the
> class_double("NewsRelease").as_null_object and got the same message. What
> am I doing wrong?
>
> /app/controller/news_configuration_controller.rb
>
>
> class NewsConfigurationController < ApplicationController
>   # GET /news_configuration/refresh_from_www
>
>   def refresh_from_www
>     NewsRelease.delete_all
>     redirect_to news_configuration_path
>   end
> end
>
>
> /spec/controller/news_configuration_controller_spec.rb
>
>
> RSpec.describe NewsConfigurationController, :type => :controller do
>
>   describe "GET 'refresh_from_www'" do
>     it "refreshes news releases" do
>       news_release_class = class_spy("NewsRelease")
>       get :refresh_from_www, { }, valid_session
>       expect(news_release_class).to have_received(:delete_all)
>     end
>
>
>     it "redirects to the news configuration index page" do
>       get :refresh_from_www, {}, valid_session
>       expect(response).to redirect_to(news_configuration_path)
>     end
>
>
>     it "deletes news releases - method called in the spec" do
>       release = FactoryGirl.create(:news_release)
>       puts "News release is: "
>       puts release.inspect
>       news_release_class = class_spy("NewsRelease")
>       puts "The class spy is: "
>       puts news_release_class.inspect
>       # get :refresh_from_www, { }, valid_session
>
>       items_deleted = NewsRelease.delete_all
>       puts "Deleted #{items_deleted} releases"
>       expect(news_release_class).to have_received(:delete_all)
>     end
>   end
> end
>
> $ rspec -f doc spec/controllers/news_configuration_controller_spec.rb:60
> Run options: include
> {:locations=>{"./spec/controllers/news_configuration_controller_spec.rb"=>[60]}}
>
> NewsConfigurationController
>   GET 'refresh_from_www'
>     refreshes news releases (FAILED - 1)
>
>     redirects to the news configuration index page
>
> News release is:
> #<NewsRelease id: 2011, title: "News Story", link: "http://www.caltech.edu";,
> description: "Short blub", pubDate: "2014-08-24 23:17:28", guid: 1,
> created_at: "2014-09-07 23:17:28", updated_at: "2014-09-07 23:17:28">
> The class spy is:
> #<RSpec::Mocks::ClassVerifyingDouble:0x98c1c58 @name="NewsRelease">
> Deleted 1 releases
>     deletes news releases - method called in the spec (FAILED - 2)
>
> Failures:
>
>   1) NewsConfigurationController GET 'refresh_from_www' refreshes news
> releases
>      Failure/Error: expect(news_release_class).to
> have_received(:delete_all)
>        (Double "NewsRelease").delete_all(any args)
>            expected: 1 time with any arguments
>            received: 0 times with any arguments
>      # ./spec/controllers/news_configuration_controller_spec.rb:64:in
> `block (3 levels) in <top (required)>'
>
>   2) NewsConfigurationController GET 'refresh_from_www' deletes news
> releases - method called in the spec
>      Failure/Error: expect(news_release_class).to
> have_received(:delete_all)
>        (Double "NewsRelease").delete_all(any args)
>            expected: 1 time with any arguments
>            received: 0 times with any arguments
>      # ./spec/controllers/news_configuration_controller_spec.rb:82:in
> `block (3 levels) in <top (required)>'
>
> Finished in 0.21353 seconds (files took 2.94 seconds to load)
> 3 examples, 2 failures
>
> Failed examples:
>
> rspec ./spec/controllers/news_configuration_controller_spec.rb:61 #
> NewsConfigurationController GET 'refresh_from_www' refreshes news releases
> rspec ./spec/controllers/news_configuration_controller_spec.rb:72 #
> NewsConfigurationController GET 'refresh_from_www' deletes news releases
> - method called in the spec
>
>
> Gemflie.lock ......
>     rspec-core (3.1.1)
>       rspec-expectations (3.1.0)
>       rspec-mocks (3.1.0)
>       rspec-rails (3.1.0)
>
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/121297c5-1422-4420-add1-4546073a2b10%40googlegroups.com
> <https://groups.google.com/d/msgid/rspec/121297c5-1422-4420-add1-4546073a2b10%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/553AE5FEAE7B4102B6CC2DAC6E17FF1B%40jonrowe.co.uk
> <https://groups.google.com/d/msgid/rspec/553AE5FEAE7B4102B6CC2DAC6E17FF1B%40jonrowe.co.uk?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Cynthia Kiser
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/CAAgeBMYXVFiERkDecD88CW16oP2EhQbrK5Ti5RJkzhehUBETsg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to