javascript - Jasmine shared specs scoping issues with coffeescript -
i'm attempting dry jasmine tests extracting out shared examples.
@sharedexamplesforthing = (thing) -> beforeeach -> @thingy = new thing "is neat", -> expect(@thingy.neat).tobetruthy() describe "widget shared behavior", -> sharedexamplesforthing(-> new widget) this works nicely when defined in 1 file. problems i'm encountering occur when try move sharedexamples separate file. can't find variable: sharedexamplesforthing ...
so in interest of debugging, tried following:
describe "widget shared behavior", -> "is acting meany", -> console.log sharedexamplesforthing expect(false).tobetruthy() sharedexamplesforthing(-> new widget) in is acting meany block, log shows sharedexamplesforthing [function] still can't find variable outside it. feel might have scoping issue outside of current experience, wrong that. missing here?
(using rails, jasminerice, guard-jasmine)
i found piece on shared examples thoughtbot useful.
i implemented in coffeescript follows:
1) in helper file loaded before spec files:
window.foospec = sharedexamples: {} window.itshouldbehavelike = (-> examplename = _.first(arguments) examplearguments = _.select(_.rest(arguments), ((arg) => return !_.isfunction(arg))) innerblock = _.detect(arguments, ((arg) => return _.isfunction(arg))) examplegroup = foospec.sharedexamples[examplename] if(examplegroup) return describe(examplename, (-> examplegroup.apply(this, examplearguments) if(innerblock) innerblock() ) ) else return it("cannot find shared behavior: '" + examplename + "'", (-> expect(false).toequal(true) ) ) ) 2) specs:
(a) can define shared behaviour:
foospec.sharedexamples["had day"] = (-> "finds code examples", -> expect(this.thoughtbot_spy).tohavebeencalled() ) (b) , use anywhere in spec as:
itshouldbehavelike("had day") (note: assuming spec has defined this.thoughtbot_spy accordingly before above line)
Comments
Post a Comment