FactoryGirl create vs. build

Problem: we have the before_save callback for a provider, and we want to test whether this callback is executed correctly.

The test passes

    context 'valid provider' do
      let(:provider) { FactoryGirl.build(:provider) }

      it 'get triggered when saved' do
        expect(provider).to receive(:test_before_save_callback)
        provider.save!
      end
    end

Even when we force the creation of provider with let, the test still passes.

    context 'valid provider' do
      let!(:provider) { FactoryGirl.build(:provider) }

      it 'get triggered when saved' do
        expect(provider).to receive(:test_before_save_callback)
        provider.save!
      end
    end

 

What I learned this week [1]

rpec

aggreate_failures

Documentation from relish about aggregate_failures

I found it so cool how rspec:

  • execute all the expectation in the group. That way, we can run through all the expectation within one example . It makes sense for all the independent expectation to be executed all, so we can have a better understanding of the test.
  • Also, the way rspec groups and displays error.

Also, the documentation of Relish – RSpec Core 3.5 is much more readable than the official documentation from RSpec core 3.5

shared_examples

A nice way to test multiple objects with slightly different behaviors. Their same behaviours can be grouped into shared_examples