Re: [rspec] Trouble using the new class_spy feature

9 views
Skip to first unread message

Jon Rowe

unread,
Sep 7, 2014, 7:25:33 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 so it will override the constant when you call it from your controller. 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
---------------------------

On Monday, 8 September 2014 at 9:04, Cynthia Kiser wrote:

I am trying to test that a particular Rails controller action deletes all items and then recreates them from an RSS feed. The new class_spy method seemed just perfect for what I was trying to do but I can't get it to work. What am I misunderstanding from the docs?

/app/controllers/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/controllers/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



--
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/4447f117-4857-46eb-9c49-44fab1ca5bfb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages