Skip to content

Default value (object of type Proxy) can give false positives when testing validations #165

@SergiiVolodkoWorking

Description

@SergiiVolodkoWorking

Hi,
Thanks for an amazing lib! It definitely makes work with JavaScript more human-friendly! :)
I've bumped into a small difference with NSubstitute behaviour and wondering if maybe I just can't find a proper syntax to turn on or off a setting to make things work similarly to what I used to in C#:)

So what I'm trying to achieve.
I have an interface

interface Worker {
   SUPPORTED_WORK: number[];
   doSomething();
}

And this kind of usage of the interface in some service:

constructor (private worker: Worker) {}
....

public someServiceMethod(workId) {
   if (!this.worker.SUPPORTED_WORK.includes(workId)) {
      throw Error("Invalid work");
   }
...

Currently, when I setup a test to cover this validation, if I don't specifying any behaviour for SUPPORTED_WORK property, the test automatically passes.

const worker = Substitute.for<Worker>();
const service = new MyService(worker);

service.someServiceMethod(42);
...//some test logic

Basing on the NSubstitute behaviour, I would expect this usage to throw an error, since the SUPPORTED_WORK property is missing or .include behaviour returns null or something like that by default.

At the moment, if the mock is unset .include(workId) returns a Proxy object, which is truthy. It makes the if-statement pass and giving a false positive in my test.
Even if I wrap this check into some method like:

   if (!this.worker.isWorkSupported(workId)) {
      throw Error("Invalid work");
   }

The issue remains.

Wondering, if I can just tweak some setting to prevent false positives in that kind of checks when I miss to setup returns of my mock?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions