Skip to content

Instantly share code, notes, and snippets.

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 hmayer00/7953530 to your computer and use it in GitHub Desktop.
Save hmayer00/7953530 to your computer and use it in GitHub Desktop.
# The base is stolen from @gist: https://gist.github.com/gr2m/2191748, and then substantially modified.
beforeEach ->
@addMatchers
toBePromise: ->
@actual.done && !@actual.resolve
toBeRejected: ->
@actual.state() == "rejected"
toBeResolved: ->
@actual.state() == "resolved"
toBeResolvedWith: ->
expectedArgs = jasmine.util.argsToArray(arguments)
unless @actual.done
throw new Error("Expected a promise, but got #{jasmine.pp(@actual)}.")
done = jasmine.createSpy 'done'
@actual.done done
@message = ->
if done.callCount == 0
return [
"Expected spy #{done.identity} to have been resolved with #{jasmine.pp(expectedArgs)} but it was never resolved.",
"Expected spy #{done.identity} not to have been resolved with #{jasmine.pp(expectedArgs)} but it was."
]
else
return [
"Expected spy #{done.identity} to have been resolved with #{jasmine.pp(expectedArgs)} but was resolved with #{jasmine.pp(done.argsForCall)}",
"Expected spy #{done.identity} not to have been resolved with #{jasmine.pp(expectedArgs)} but was resolved with #{jasmine.pp(done.argsForCall)}"
]
return @env.contains_(done.argsForCall, expectedArgs)
toBeRejectedWith: ->
expectedArgs = jasmine.util.argsToArray(arguments)
unless @actual.fail
throw new Error("Expected a promise, but got #{jasmine.pp(@actual)}")
fail = jasmine.createSpy 'fail'
@actual.fail fail
@message = ->
if fail.callCount == 0
return [
"Expected spy #{fail.identity} to have been rejected with #{jasmine.pp(expectedArgs)} but it was never rejected.",
"Expected spy #{fail.identity} not to have been rejected with #{jasmine.pp(expectedArgs)} but it was."
]
else
return [
"Expected spy #{fail.identity} to have been rejected with #{jasmine.pp(expectedArgs)} but was rejected with #{jasmine.pp(fail.argsForCall)}",
"Expected spy #{fail.identity} not to have been rejected with #{jasmine.pp(expectedArgs)} but was rejected with #{jasmine.pp(fail.argsForCall)}"
]
return @env.contains_(fail.argsForCall, expectedArgs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment