Home > Software engineering >  Rails Rspec: Test that service is called
Rails Rspec: Test that service is called

Time:04-07

I am trying to write a test to ensure that my service, WeeklyReportCardService, is instantiated and that it's method :send_weekly_report_card_for_repositioning is called.

Here's the controller:

def update
    Audited.audit_class.as_user($user) do
        if @check_in.update(check_in_params)
            client = Client.find_by(id: check_in_params[:client_id])

            if @check_in.repositioning.present? && @check_in.weigh_in.present? && @check_in.client&.location&.name == "World Wide"

                # I see this in the console so the if statement returns true
                p "hitting send!!"

                WeeklyReportCardService.new.send_weekly_report_card_for_repositioning(@check_in.repositioning)
            end

            render json: @check_in, status: :ok, serializer: API::CheckInsIndexSerializer
        else
            render json: @check_in.errors.full_messages, sattus: :unprocessable_entity
        end
    end
end

Here's my test:

RSpec.describe API::CheckInsController, type: :request do
    fit "should send if the client's location is World Wide" do
        program = create(:program, :with_client)
        worldwide = create(:location, name: "World Wide")
        program.client.update(location_id: worldwide.id)
        check_in = create(:check_in, client_id: program.client.id, program_id: program.id)
        create(:repositioning, check_in_id: check_in.id)
        create(:weigh_in, check_in_id: check_in.id)
        
        url = root_url[0..-2]   api_check_in_path(check_in.id)   "?sendReportCardEmail=true"

        put url, params: { check_in: {type_of_weighin: 'standard'}}, headers: { "HTTP_AUTHENTICATION": @token }
        expect_any_instance_of(WeeklyReportCardService).to receive(:send_weekly_report_card_for_repositioning)
    end
end

and the error I see is:

 Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure }
   Exactly one instance should have received the following message(s) but didn't: send_weekly_report_card_for_repositioning

What else do I need to do to ensure that function is called?

CodePudding user response:

You're doing it in the wrong order. You need to set the expectation first before the method is expected to be called:

expect_any_instance_of(WeeklyReportCardService).to receive(:send_weekly_report_card_for_repositioning)
put url, params: { check_in: {type_of_weighin: 'standard'}}, headers: { "HTTP_AUTHENTICATION": @token }

If you need to set the expecations afterwards you need to replace the method or object with a spy which is useful if you prefer the arrange-act-assert (or given-when-then) pattern for structuring tests.

You should also note that the use of any instance is strongly discouraged and you can avoid it by providing a simple class method:

class WeeklyReportCardService
  def self.send_weekly_report_card_for_repositioning(...)
    new.send_weekly_report_card_for_repositioning(...)
  end
end
RSpec.describe API::CheckInsController, type: :request do
  it "should send if the client's location is World Wide" do
    expect(WeeklyReportCardService).to receive(:send_weekly_report_card_for_repositioning)
    put url, params: { check_in: {type_of_weighin: 'standard'}}, headers: { "HTTP_AUTHENTICATION": @token }
  end
end

Or alternatively by stubbing the WeeklyReportCardService#new method to return a mock or spy.

  • Related