Trouble using the new class_spy feature

466 views
Skip to first unread message

Cynthia Kiser

unread,
Sep 7, 2014, 7:31:38 PM9/7/14
to rs...@googlegroups.com
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)



Jon Rowe

unread,
Sep 7, 2014, 7:38:22 PM9/7/14
to rs...@googlegroups.com
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
---------------------------

--
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 rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/121297c5-1422-4420-add1-4546073a2b10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cynthia Kiser

unread,
Sep 7, 2014, 11:31:51 PM9/7/14
to rs...@googlegroups.com
Thanks. I hadn't used class_double so I didn't know it needed .as_stubbed_const to work in my context.


For more options, visit https://groups.google.com/d/optout.



--
Cynthia Kiser
cynthi...@gmail.com
Reply all
Reply to author
Forward
0 new messages