mercredi 7 octobre 2015

Jasmine and.callThrough/ Mocking other module within tested function

I have a unit test for my testService. I call a function create which calls another subfunction save which mirrors (apiService.send) the data to a national server. In my unit test I don't want to test the connection to a server etc. so I want to mock the function apiService.send and instead always return a certain value. But I still want to keep the rest of the save function, because I really want to check if everything was saved well in the database.

As far as I read Jasmine - How to spy on a function call within a function? this can be achieved with and.callThrough

test

testService.createTest(appUser, testData, (err, test) ->
  expect(err).toBe(null)
  ...

function

saveTest = (test, method, appUserToken, callback) ->
  async.parallel
    local: (next)->
      test.save((err) ->
        return next err if err?
        return next null, test._id
      )
    national: (next)->
      apiService.send(environment, method, test, appUserToken, (err, testId) ->
        return next err if err?
        TestModel.update({_id: test._id},  { $set: { refId: new Object(testId) }}, (err, result) ->
          return next err if err?
          return next 'Referenz Id wurde nicht gespeichert' if result.nModified==0
          return next null, test._id
        )
      )
    (err, results)->
      return callback err if err?
      return callback null, results.local


exports.createTest = (appUser, testData, callback) ->
  ...
  saveTest(newTest, 'createTest', appUser.token, callback)

Aucun commentaire:

Enregistrer un commentaire