Skip to content

Instantly share code, notes, and snippets.

@ignar
Created June 15, 2017 15:12
Show Gist options
  • Save ignar/9a7b4bdf83055a6b36c32ca610402f1a to your computer and use it in GitHub Desktop.
Save ignar/9a7b4bdf83055a6b36c32ca610402f1a to your computer and use it in GitHub Desktop.
Brandslisten GmbH Test Assignment

Brandslisten GmbH Test Assignment

Let's assume, that got back into dark ages when developers haven't FactoryGirl gem and had to use fixtures instead.

You are first one, who understood that fixtures are not so handy as other think they are. You decide to write you own library and open source it for the world. FactoryBoy is looking like a good name.

You started from the simple class that you want to test and looks as simple as it is:

class User
  attr_accessor :name, :email
end

You really want to have each step of implementation as a separate commit in the repo for followers and just to keep history clean. You are free to use any test framework, but what you repo followers are expecting is a fair test coverage.

At the first step that you want to try is to implement the next:

FactoryBoy.define_factory(User)
user = FactoryBoy.build(User) # => #<User:0x007fb99a8d41d0>

Simple as a pie.

At the second step, you decide that it goes fine, but you don't really want to use constants for object building, it is much better to pass symbols and string, e.g.:

FactoryBoy.define_factory(:user)
user = FactoryBoy.build(:user) # => #<User:0x007fb99a81c1c0>

At the third step, when your application becomes larger and logic becomes not so linear, you want to use different roles for the same model:

FactoryBoy.define_factory(:admin, class: User)
user = FactoryBoy.build(:admin) # => #<User:0x007fb99a271db0>

At the fourth step, you decide to add parameters during object building:

FactoryBoy.define_factory(:user, name: ‘John’)
user = FactoryBoy.build(:user) # => #<User:0x007fb99a278908 @name="John">
user = FactoryBoy.build(:user, name: 'Franklin') # => #<User:0x007fb99b8495c0 @name="Franklin">

At the fifth step, by many requests of repo followers, you implement next advanced feature:

FactoryBoy.define_factory(User) do
  name ‘John’
end

user = FactoryBoy.build(:admin, email: 'john@gmail.com') # => #<User:0x007fb99b860a68 @name="John", @email="john@gmail.com">

But in any case, it doesn't make sense to spend more that 4 hours on this, it is just a hobby, isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment