Search code examples
ruby-on-railsrspecrspec2rspec-rails

How to test uniqueness of Coupon/Promo-Codes?


I have a Model PromoCode which has a .generate! method, that calls .generate which generates a String using SecureRandom.hex(5) and saves it to the database:

class PromoCode < ActiveRecord::Base
  class << self
    def generate 
      SecureRandom.hex 5
    end

    def generate!
      return create! code: generate
    end
  end
end

Now I want to write a spec that test the uniqueness of the generated string. The .generate method should be called as long as a non existent PromoCode has been generated.

I'm not sure how to do this since I can't really stub out the .generate method to return fixed values (because then it would be stuck in an infinite loop).

This is the passing spec for the model so far:

describe PromoCode do
  describe ".generate" do
    it "should return a string with a length of 10" do
      code = PromoCode.generate
      code.should be_a String
      code.length.should eql 10
    end
  end

  describe ".generate!" do
    it "generates and returns a promocode" do
      expect {
        @promo = PromoCode.generate!
      }.to change { PromoCode.count }.from(0).to(1)

      @promo.code.should_not be_nil
      @promo.code.length.should eql 10
    end

    it "generates a uniq promocode" do
    end
  end
end

Any directions appreciated.


Solution

  • Rspec's and_return method allows you to specify multiple return values that will be cycled through

    For example you could write

    PromoCode.stub(:generate).and_return('badcode1', 'badcode2', 'goodcode')
    

    Which will cause the first call to generate to return 'badcode1', the second 'badcode2' etc... You can then check that the returned promocode was created with the correct code.

    If you want to be race condition proof you'll want a database uniqueness constraint, so your code might actually want to be

    def generate!
       create!(...)
    rescue ActiveRecord::RecordNotUnique
      retry
    end
    

    In your spec you would stub the create! method to raise the first time but return a value the second time