Skip to content

Instantly share code, notes, and snippets.

@shwoodard
Created March 22, 2013 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shwoodard/5225556 to your computer and use it in GitHub Desktop.
Save shwoodard/5225556 to your computer and use it in GitHub Desktop.
describe "When using a Factory", ->
it "has a name", ->
factory = new Factory("factoryName")
expect(factory.name).toEqual("factoryName")
describe "configuration", ->
factory = null
beforeEach ->
factory = new Factory "factory", (config) ->
config.setFields(["id", "name", "position"])
it "it's fields are configurable", ->
factoryFields = _.sortBy _.keys(factory.fields), (val) -> val
expect(factoryFields).toEqual(["id", "name", "position"])
it "allows a default value", ->
factory.setDefault("name", "foo")
expect(JSON.parse(factory.toJson())).toEqual
id: null
name: "foo"
position: null
it "allows a basic numerical sequence", ->
factory.setDefault("id", 1, true)
_.each [1,2], (times) ->
expect(JSON.parse(factory.toJson())).toEqual
id: times
name: null
position: null
it "allows a sequence with a custom funtion", ->
factory.setDefault "name", 1, (n) ->
"Foo #{n}"
_.each [1,2], (times) ->
expect(JSON.parse(factory.toJson())).toEqual
id: null
name: "Foo #{times}"
position: null
it "allows you to pass in overrides", ->
factory = new Factory "factory", (config) ->
config.setFields(["id", "name", "position"])
config.setDefault "id", 1, true
config.setDefault "name", 1, (n) ->
"Story #{n}"
config.setDefault "position", 1, true
expect(JSON.parse(factory.toJson(id: 4))).toEqual
id: 4
name: "Story 1"
position: 1
expect(JSON.parse(factory.toJson(name: "Foo"))).toEqual
id: 2
name: "Foo"
position: 2
expect(JSON.parse(factory.toJson(position: 11))).toEqual
id: 3
name: "Story 3"
position: 11
expect(JSON.parse(factory.toJson())).toEqual
id: 4
name: "Story 4"
position: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment