Skip to content

Instantly share code, notes, and snippets.

@jsborjesson
Created March 18, 2014 09:04
Show Gist options
  • Select an option

  • Save jsborjesson/9616273 to your computer and use it in GitHub Desktop.

Select an option

Save jsborjesson/9616273 to your computer and use it in GitHub Desktop.
Easy testing of json API:s with Factory Girl
class JsonForStrategy
def initialize
@strategy = FactoryGirl.strategy_by_name(:build).new
end
delegate :association, to: :@strategy
def result(evaluation)
@strategy.result(evaluation).to_json
end
end
FactoryGirl.register_strategy(:json_for, JsonForStrategy)
@jsborjesson
Copy link
Author

Dump this in your support/ and use it like every other factory:

FactoryGirl.json_for(:my_model)
FactoryGirl.json_for(:my_model, my_attr: 'value')

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