While working with any application, testing plays most important role. Data is main requirement for writing proper test cases. We need different kind of data to test complex features and here factories plays important role.
Data factory is blueprint that allows us to create an object, or a collection of objects, with predefined set of values. Factories makes it easy for us to define different kind of data.
In rails or ember we create model object to write test cases. In rails many of us use FactoryGirl
to create such test data using factories. Same way ember has FactoryGuy
. Ember's FactoryGuy provides almost all functionalities which Rails FactoryGirl provides. Using FactoryGuy becomes easy to understand ember test cases if you have already used FactoryGirl in rails.
FactoryGuy has good documentation which explain how to start with FactoryGuy in ember. FactoryGuy Documentation
Lets have a quick look on similar things which rails FactoryGirl and ember FactoryGuy provides.
1 - Both provides creation of sequences.
FactoryGirl:
FactoryGirl.define do
sequence :email do |n|
"person#{n}@example.com"
end
end
FactoryGuy:
FactoryGuy.define('user', {
sequences: {
userName: (num)=> `User${num}`
}
});
2 - Traits allows us to create different sets of data with common values from main factory. Both provides trait which is great.
FactoryGirl:
factory :story do
title "My awesome story"
trait :published do
published true
end
end
FactoryGuy:
FactoryGuy.define('story', {
default: {
title: "My awesome story"
},
traits: {
published: { published: true }
}
3 - Both provides create and build for object.
FactoryGirl:
user = create(:user)
user = build(:user)
FactoryGuy:
let user = make('user');
let user = build('user');
4 - Both provides creation and building lists.
FactoryGirl:
built_users = build_list(:user, 25)
created_users = create_list(:user, 25)
FactoryGuy:
let users = FactoryGuy.makeList('users', 2);
let users = FactoryGuy.buildList('users', 2);
These are the few basic similarities which helps us to start with ember testing if already aware and worked with rails testing with rspec and FactoryGirl. And it's gives us confidence to work effectively with ember testing. Hope it will help you those who are from rails background and starting to write test cases in ember.