0
class foo {
    constructor() {
        this._token = '';
}

I'm trying to mock an API call, and I want to inject a mocked value for this._token. When a mocked value is hardcoded here, my mocked API call runs properly and my unit test passes.

I would like to somehow inject a value here. Is this possible?

user3019802
  • 65
  • 2
  • 10
  • Why do you need to mock it? It doesn't take any values, does foo have any other behaviour? Can you just use `{ _token: whatever }`? – jonrsharpe Sep 24 '20 at 21:08
  • @jonrsharpe The foo calls two internal functions before populating the token value. One function is an API call which grabs a token, another function returns it to the function which calls an API that needs that token value. The API call that needs a token value can be successfully mocked when that token is populated with a dummy value. Otherwise, it throws an error. – user3019802 Sep 24 '20 at 21:15
  • But why do you need to mock that? What are you actually testing? A collaborator? Foo itself? Give a [mre]. – jonrsharpe Sep 24 '20 at 21:16
  • For cases like this, I would use Nock to simply control the token value that is returned. However, since the question is worded explicitly for mocking an attribute with Sinon, I keep to that. Sinon mocks attrs on class instances/objects. So we need to know more about how your foo class is used in order to provide an answer. – Matt R. Wilson Sep 24 '20 at 21:19
  • @MattR.Wilson. So, I was actually using nock. The class makes two post calls, and I can't seem to return the dummy token for the first post call. (which would populate the token variable when the code is running), and then within that same class, that token is used for another API call to send a text message. First Post call for a token, second for the call to send the message. – user3019802 Sep 24 '20 at 21:33
  • I think the issue might be related to the service making two post calls and there's an issue with Nock in that it can't mock both calls. It'll mock one and then do a real request next. https://github.com/nock/nock/issues/997 – user3019802 Sep 25 '20 at 00:22
  • The linked Nock issue was not related to multiple requests in the same test. I assure you, Nock can handle managing many expected requests at once. – Matt R. Wilson Sep 25 '20 at 18:03
  • @MattR.Wilson. Are multiple requests of the same type possible? I'm trying to do two POST calls to different endpoints in the same Unit Test. One POST grabs an Auth, another is supposed to make a post call to send a text message. When I try to mock both, it actually tries to make an API call. I can only mock one atm. Should I be using persist if doing multiple requests at once? – user3019802 Sep 26 '20 at 01:33
  • Nock has no limits how many, similar requests it can mock. It sounds like your mock isn't right. I recommend using `disableNetConnect` and checking out the debugging section of Nock's docs, so see why the second request is making a live call. – Matt R. Wilson Sep 26 '20 at 02:52
  • Thank you for your suggestions. I think it might have been an issue with Mocha and Nock. https://stackoverflow.com/questions/41619376/how-do-i-make-nock-and-mocha-play-well-together I put my mock calls to this service in BeforeEach in describe and all my tests pass. Maybe Mocha and Nock don't jive well, or there's some issue with hooks? – user3019802 Sep 26 '20 at 04:47

0 Answers0