Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test your Config/Initializer Scripts with Rspec in Rails?

So I was searching online for a solution, but there seems to be scarce information about testing initializers created in Rails.

At the moment, I've written a pretty large API call-n-store in my config/initializers/retrieve_users.rb file. It makes an API request, parses the JSON, and then stores the data as users. Being pretty substantial, I've yet to figure out the cleanest way to test it. Since I need to retrieve the users before any functions are run, I don't believe I can move this script anywhere else (although other suggestions would be welcomed). I have a couple questions about this:

  1. Do I have to wrap this in a function/class to test my code?
  2. Where would I put my spec file?
  3. How would I format it RSpec-style?

Thanks!

like image 901
ForgetfulFellow Avatar asked Aug 05 '14 19:08

ForgetfulFellow


People also ask

What is RSpec testing?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

Is RSpec a unit test?

RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the "behavior" of an application being tested.

What are Initializers in rails?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.


1 Answers

I just gave this a shot and have passing rspec tests, I am going to further refactor my code and in the end it will be shorter, but here is a snapshot of what I did, using this link as a guide:

The gist of it is this: make a class in your initializer file with the functions you want, then write tests on the functions in the class.

config/initializers/stripe_event.rb

StripeEvent.configure do |events|
  events.subscribe 'charge.dispute.created' do |event|
    StripeEventsResponder.charge_dispute_created(event)
  end
end

class StripeEventsResponder
  def self.charge_dispute_created(event)
    StripeMailer.admin_dispute_created(event.data.object).deliver
  end
end

spec/config/initializers/stripe_events_spec.rb

require 'spec_helper'

describe StripeEventsResponder do
  before { StripeMock.start }
  after { StripeMock.stop }
  after { ActionMailer::Base.deliveries.clear }

  describe '#charge_dispute_created' do
    it "sends one email" do
      event = StripeMock.mock_webhook_event('charge.dispute.created')
      StripeEventsResponder.charge_dispute_created(event)
      expect(ActionMailer::Base.deliveries.count).to eq(1)
    end

    it "sends the email to the admin" do
      event = StripeMock.mock_webhook_event('charge.dispute.created')
      StripeEventsResponder.charge_dispute_created(event)
      expect(ActionMailer::Base.deliveries.last.to).to eq(["[email protected]"])
    end
  end
end
like image 148
user3291025 Avatar answered Oct 27 '22 00:10

user3291025