kronosapiens.github.io - Setting up a test suite with FactoryGirl and Faker









Search Preview

Setting up a test suite with FactoryGirl and Faker

kronosapiens.github.io
Note: this is my first post written using markdown. I’ve decided to migrate my technical blog back to Octopress from Wordpress after realizing that Wordpress...
.io > kronosapiens.github.io

SEO audit: Content analysis

Language Error! No language localisation is found.
Title Setting up a test suite with FactoryGirl and Faker
Text / HTML ratio 43 %
Frame Excellent! The website does not use iFrame solutions.
Flash Excellent! The website does not have any flash contents.
Keywords cloud test end FactoryGirl title factory create objects define createlesson lesson trait status object attributes tests values I’ve suite attribute case
Keywords consistency
Keyword Content Title Description Headings
test 17
end 12
FactoryGirl 11
title 10
factory 10
create 9
Headings
H1 H2 H3 H4 H5 H6
2 8 0 0 0 0
Images We found 0 images on this web page.

SEO Keywords (Single)

Keyword Occurrence Density
test 17 0.85 %
end 12 0.60 %
FactoryGirl 11 0.55 %
title 10 0.50 %
factory 10 0.50 %
create 9 0.45 %
objects 9 0.45 %
define 8 0.40 %
createlesson 8 0.40 %
lesson 7 0.35 %
trait 7 0.35 %
status 7 0.35 %
object 7 0.35 %
attributes 6 0.30 %
tests 6 0.30 %
values 5 0.25 %
I’ve 5 0.25 %
suite 5 0.25 %
attribute 5 0.25 %
case 5 0.25 %

SEO Keywords (Two Word)

Keyword Occurrence Density
of our 5 0.25 %
test suite 5 0.25 %
of the 5 0.25 %
this case 4 0.20 %
need to 4 0.20 %
= createlesson 4 0.20 %
all the 4 0.20 %
do status 4 0.20 %
when we 4 0.20 %
which is 3 0.15 %
the factory 3 0.15 %
test file 3 0.15 %
you need 3 0.15 %
for the 3 0.15 %
out of 3 0.15 %
20140427 225456 3 0.15 %
to define 3 0.15 %
lets you 3 0.15 %
going on 3 0.15 %
values for 3 0.15 %

SEO Keywords (Three Word)

Keyword Occurrence Density Possible Spam
status 'closed' end 2 0.10 % No
FakerNamename nto_s end 2 0.10 % No
specific_time DateTimenow specific_location 2 0.10 % No
FakerInterneturl specific_time DateTimenow 2 0.10 % No
references FakerInterneturl specific_time 2 0.10 % No
FakerLoremsentence references FakerInterneturl 2 0.10 % No
description FakerLoremsentence references 2 0.10 % No
title description FakerLoremsentence 2 0.10 % No
do title description 2 0.10 % No
lesson do title 2 0.10 % No
factory lesson do 2 0.10 % No
when we create 2 0.10 % No
n FakerNamename nto_s 2 0.10 % No
trait completed do 2 0.10 % No
do n FakerNamename 2 0.10 % No
title do n 2 0.10 % No
sequence title do 2 0.10 % No
to define a 2 0.10 % No
what’s going on 2 0.10 % No
factory In this 2 0.10 % No

SEO Keywords (Four Word)

Keyword Occurrence Density Possible Spam
do status 'completed' end 2 0.10 % No
lesson do title description 2 0.10 % No
trait completed do status 2 0.10 % No
specific_time DateTimenow specific_location FakerAddressstreet_address 2 0.10 % No
references FakerInterneturl specific_time DateTimenow 2 0.10 % No
FakerLoremsentence references FakerInterneturl specific_time 2 0.10 % No
description FakerLoremsentence references FakerInterneturl 2 0.10 % No
title description FakerLoremsentence references 2 0.10 % No
do title description FakerLoremsentence 2 0.10 % No
factory lesson do title 2 0.10 % No
status 'completed' end trait 2 0.10 % No
n FakerNamename nto_s end 2 0.10 % No
do n FakerNamename nto_s 2 0.10 % No
title do n FakerNamename 2 0.10 % No
sequence title do n 2 0.10 % No
an attribute in this 2 0.10 % No
attribute in this case 2 0.10 % No
factory In this case 2 0.10 % No
completed do status 'completed' 2 0.10 % No
FakerInterneturl specific_time DateTimenow specific_location 2 0.10 % No

Internal links in - kronosapiens.github.io

About
About
Strange Loops and Blockchains
Strange Loops and Blockchains
Trie, Merkle, Patricia: A Blockchain Story
Trie, Merkle, Patricia: A Blockchain Story
Reputation Systems: Promise and Peril
Reputation Systems: Promise and Peril
The Future of Housing, in Three Parts
The Future of Housing, in Three Parts
Proof of Work vs Proof of Stake: a Mirror of History
Proof of Work vs Proof of Stake: a Mirror of History
Introducing Talmud
Introducing Talmud
The Economics of Urban Farming
The Economics of Urban Farming
Time and Authority
Time and Authority
On Meaning in Games
On Meaning in Games
Objective Functions in Machine Learning
Objective Functions in Machine Learning
A Basic Computing Curriculum
A Basic Computing Curriculum
The Problem of Information II
The Problem of Information II
The Problem of Information
The Problem of Information
Elements of Modern Computing
Elements of Modern Computing
Blockchain as Talmud
Blockchain as Talmud
Understanding Variational Inference
Understanding Variational Inference
OpsWorks, Flask, and Chef
OpsWorks, Flask, and Chef
On Learning Some Math
On Learning Some Math
Understanding Unix Permissions
Understanding Unix Permissions
30 Feet from Michael Bloomberg
30 Feet from Michael Bloomberg
The Academy: A Machine Learning Framework
The Academy: A Machine Learning Framework
Setting up a queue service: Django, RabbitMQ, Celery on AWS
Setting up a queue service: Django, RabbitMQ, Celery on AWS
Versioning and Orthogonality in an API
Versioning and Orthogonality in an API
Designing to be Subclassed
Designing to be Subclassed
Understanding Contexts in Flask
Understanding Contexts in Flask
Setting up Unit Tests with Flask, SQLAlchemy, and Postgres
Setting up Unit Tests with Flask, SQLAlchemy, and Postgres
Understanding Package Imports in Python
Understanding Package Imports in Python
Setting up Virtual Environments in Python
Setting up Virtual Environments in Python
Creating superfunctions in Python
Creating superfunctions in Python
Some Recent Adventures
Some Recent Adventures
Sorting in pandas
Sorting in pandas
Mimicking DCI through Integration Tests
Mimicking DCI through Integration Tests
From Ruby to Python
From Ruby to Python
Self-Focus vs. Collaboration in a Programming School
Self-Focus vs. Collaboration in a Programming School
Designing Software to Influence Behavior
Designing Software to Influence Behavior
Maintaining Octopress themes as git submodules
Maintaining Octopress themes as git submodules
Setting up a test suite with FactoryGirl and Faker
Setting up a test suite with FactoryGirl and Faker
To Unit Test or not to Unit Test
To Unit Test or not to Unit Test
A Dynamic and Generally Efficient Front-End Filtering Algorithm
A Dynamic and Generally Efficient Front-End Filtering Algorithm
Trails & Ways: A Look at Rails Routing
Trails & Ways: A Look at Rails Routing
Getting Cozy with rspec_helper
Getting Cozy with rspec_helper
Exploring the ActiveRecord Metaphor
Exploring the ActiveRecord Metaphor
Civic Hacking as Inspiration
Civic Hacking as Inspiration
From Scheme to Ruby
From Scheme to Ruby
Setting up Auto-Indent in Sublime Text 2
Setting up Auto-Indent in Sublime Text 2
hello world
hello world
via RSS
Abacus

Kronosapiens.github.io Spined HTML


Setting up a test suite with FactoryGirl and Faker Abacus About Setting up a test suite with FactoryGirl and Faker Apr 27, 2014 Note: this is my first post written using markdown. I’ve decided to migrate my technical blog when to Octopress from Wordpress without realizing that Wordpress’s support for lawmaking snippets is just not up to snuff. Markdown is fun! Intro Since Flatiron School ended last week, I’ve been taking some time to work on xp, a skillshare-like platform I created (along with @sts10) for the Flatiron science fair. I realized early on how important it would be to wilt good at testing, and I’ve made tests a top priority in every project I’ve done. The test suite for xp is the weightier I’ve overly written, and while that isn’t saying all that much (it’s far from perfect), there are some good things going on. We made use of thoughtbot’s FactoryGirl, a gem which lets you create ‘factories’ to hands create objects to test. xp uses a lot of associations and validations, which meant that getting FactoryGirl to do what we wanted took a little bit of configuration. The TAs at Flatiron were a big help in getting everything going. Factory girl works by letting you pinpoint ‘factories’, which contain all the information and settings you need in order to create ActiveRecord objects for your test suite. Complex objects require some special settings and configuration – as we go through the factory settings, I’ll explain what aspects of our program required the spare factory configuration. Here’s one of our factories (I’ll explain what’s going on below): FactoryGirl.define do sequence :title do |n| Faker::Name.name + n.to_s end factory :lesson do title unravelment { Faker::Lorem.sentence } references { Faker::Internet.url } specific_time { DateTime.now } specific_location { Faker::Address.street_address } trait :completed do status 'completed' end trait :closed do status 'closed' end before(:create) do |lesson| lesson.tags << create(:tag) end end end Let’s take it bit by bit. Creating the factory FactoryGirl.define do # lawmaking end This may be self-explanatory, but this lawmaking is where you tell FactoryGirl to pinpoint a new factory. All of the blocks that follow are organized by the ‘define’ function into a single factory. In this case, the factory is :lesson. Defining a sequence sequence :title do |n| Faker::Name.name + n.to_s end Many ActiveRecord objects require that their name or title (or similar) nature be unique. FactoryGirl supports this by providing a syntax for defining ‘sequences’, where an symbol (in this case, :title) will be automatically incremented every time FactoryGirl creates an object out of that factory. In this case, n will increase by one every time we ask for a new object, so that no two objects have the same name. You may be wondering what’s going on with Faker::Name.name. Faker is a gem which lets you create real-looking fake data for attributes. It makes testing a lot increasingly fun (and makes your tests increasingly realistic), considering you’re dealing with values that squint like real data, versus every name and unravelment stuff some variation of ‘test test’ ;).Increasinglyon using Faker with FactoryGirl Defining values for the objects nature factory :lesson do title unravelment { Faker::Lorem.sentence } references { Faker::Internet.url } specific_time { DateTime.now } specific_location { Faker::Address.street_address } Here, we get lanugo to merchantry and pinpoint the values for all the nature of the lesson object. Note that we don’t need to pinpoint a value for every attribute. At minimum, you’ll need to satisfy every validation requirement you’ve written for your object. Everything whilom that is optional, and you’ll want to pinpoint nature as necessary so you can test what you need to test. You’ll note that title doesn’t have any value without it. That’s considering we’ve specified title earlier, when we specified the sequence. Calling title on the second line pulls a value out of the sequence generator above, while all the other nature take their values from the woodcut stuff passed to that attribute. Specifying specific traits trait :completed do status 'completed' end trait :closed do status 'closed' end Here, we pinpoint special “options” for when we create our objects. Some of our tests test for policies based on specific values for an symbol (in this case, the status attribute). We specify the trait when we create the object in our very test file (which you’ll see later). This trait full-length is very powerful, and allows you to tap into the power of FactoryGirl without sacrificing the flexibility of creating objects explicitly in your tests. Advanced deportment before(:create) do |lesson| lesson.tags << create(:tag) end Sometimes you need to take deportment on your object surpassing you can save it – in our case, we wrote a custom validator which required a lesson to have at least one associated tag surpassing it could be saved. We addressed this by defining a before(:create) action, which is tabbed without an object is instantiated but surpassing it is first saved to the database. FactoryGirl in whoopee Now that you’ve seen all the powers of FactoryGirl in defining a factory, let’s see it in action. Here’s an exerpt from our lesson_spec.rb file: describe "Lesson" do let(:lesson1){ create(:lesson) } let(:lesson2){ create(:lesson) } let(:lesson3){ create(:lesson, :closed) } let(:lesson4){ create(:lesson, :completed) } As an aside, notice that we’re using rspec’s let syntax, which is powerful considering it lets you pinpoint variables in your test file, but not unquestionably create the objects until your very test needs it. What that ways is that lesson1 is not unquestionably created until much later, when we undeniability it for the first time below: lesson1.registrations.create(:user => user1, :role => "student") The wholesomeness here is that we can pinpoint the variables all in one place, but not use system resources creating and storing them until the point at which they are required in the test. Contrast this to the following: before(:each) do @lesson1 = create(:lesson) @lesson2 = create(:lesson) @lesson3 = create(:lesson, :closed) @lesson4 = create(:lesson, :completed) end This woodcut would create all four lessons surpassing the first test is run, and regardless of whether or not the variable is called, resulting in a much slower test suite. A third option, which is defining every variable at the top of every test, would be faster at runtime, but lead to a swollen and redundant test file. The let syntax gives us speed while keeping us DRY. Use it! Going when to FactoryGirl, we see how hands we can create new lessons and configure them as necessary for out tests. One of our tests checks a method which returns all lessons by status. FactoryGirl’s trait full-length gave us the worthiness to tenancy just unbearable of the lesson’s information to pass that test, while keeping most of the data out of our hands (making our test suite increasingly valuable, by making the data as tropical to possible as real life). Finally, let’s take a peek at what lesson1 looks like: #<Lesson id: 1, title: "Mr. Madyson Mayert4", description: "Quidem eligendi non laboriosam ratione ipsam labor...", references: "http://bernhard.net/izaiah", created_at: "2014-04-27 22:54:56", updated_at: "2014-04-27 22:54:56", specific_time: "2014-04-27 22:54:56", specific_location: "60691 Milan Trail", status: "open", slug: "mr.-madyson-mayert4"> Happy testing! Comments Please enable JavaScript to view the comments powered by Disqus. Abacus Abacus kronovet@gmail.com kronosapiens kronosapiens I'm Daniel Kronovet, a data scientist living in Tel Aviv.