Testing Simple Sti with Factorygirl

Testing simple STI with FactoryGirl

You can declare the definitions of factories as follow:

FactoryGirl.define do
factory :task, class: 'Task' do
shop
currency
value 10
end

factory :counter_task, parent: :task, class: 'CounterTask' do
end
end

And now you can test base class isolated from its own factory and the same for each inherited class.

Testing simple STI with FactoryGirl

You can declare the definitions of factories as follow:

FactoryGirl.define do
factory :task, class: 'Task' do
shop
currency
value 10
end

factory :counter_task, parent: :task, class: 'CounterTask' do
end
end

And now you can test base class isolated from its own factory and the same for each inherited class.

Testing STI Associations with Shoulda-Matchers & FactoryGirl

I think you're mixing your associations up.

To test the has_many within your PracticeManager model, you simply want:

RSpec.describe PracticeManager, type: :model do
describe "Relationships" do
it { should have_many(:providers) }
end
end

If you want to test your Provider model, then you'd need the extra options:

RSpec.describe Provider, type: :model do
it { should belong_to(:practice_manager).class_name('User').with_foreign_key(:user_id) }
end

Testing dynamic initial states with FactoryGirl and StateMachine

You may be able to just add an after_build callback on your factory:

Factory.define :car do |c|
c.after_build { |car| car.initialize_state }
end

However, I don't think you should rely on setting your initial state in this way. It is very common to use ActiveRecord objects like FactoryGirl does (i.e. by calling c = Car.net; c.my_column = 123).

I suggest you allow your initial state to be nil. Then use an active record callback to set the state to to the desired value.

class Car < ActiveRecord::Base
attr_accessor :stolen # This would be an ActiveRecord attribute

state_machine do
state :parked, :moving
end

before_validation :set_initial_state, :on => :create

validates :state, :presence => true

private
def set_initial_state
self.state ||= stolen ? :moving : :parked
end
end

I think this will give you more predictable results.

One caveat is that working with unsaved Car objects will be difficult because the state won't be set yet.

RSpec / FactoryGirl - Rails STI - equality

You can force the superclass factory to return an instance of the subclass by using initialize_with

Ex:

initialize_with do
klass = type.constantize
klass.new(attributes)
end

Single Table Inheritance with Factory Girl in Rails

Change:

let(:video_collection) do 
FactoryGirl.create(:video_collection)
end

to:

let!(:video_collection) do 
FactoryGirl.create(:video_collection)
end

The difference between the two is the ! (exclamation point) after the let

Testing STI base class in isolation with Fabricator

So after discussing with a coworker, we came to the following solution.

In the spec file, I added an empty dummy class and inherited from my base class. I can then test using this dummy class. Since the dummy class is empty the only logic that gets tested is the base class. I can then add/remove subclasses without fear of breaking the base class tests.

Testing for FactoryGirl Results

You can't do it this way. You'll have to check each record individually like this

it "should sort the items in order" do
expect(thing[0].ordering).to eq(1)
expect(thing[1].ordering).to eq(2)
expect(thing[2].ordering).to eq(3)
end

Or do something like this:

it "should sort the items in order" do
expect(thing.map(&:ordering)).to eq([1, 2, 3])
end

You can only use include to check if the array includes an element as a whole, like this:

expect(thing).to include(thing[0])


Related Topics



Leave a reply



Submit