# \[HELP\] Unit tests

**URL:** <https://community.gladysassistant.com/t/help-tests-unitaire/6170>\
**Category:** Développement\
**Created:** [March 12, 2021, 11:00am UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170 "2021-03-12T11:00:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [March 12, 2021, 11:00am UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/1 "2021-03-12T11:00:21Z")

</div>

Sorry in advance if my questions are basic (for professional devs)

@cicoub13 gave me a hand 🙏 on the [PR](https://github.com/GladysAssistant/Gladys/pull/1045) concerned. but being a noob, I can’t figure it out

I need to test this file:  
[https://github.com/GladysAssistant/Gladys/pull/1045/files#diff-1e5ef582cb3ebc70e2b396ec31c6116178f1a41b6434f178ae83f0fdd0350ca0](https://github.com/GladysAssistant/Gladys/pull/1045/files#diff-1e5ef582cb3ebc70e2b396ec31c6116178f1a41b6434f178ae83f0fdd0350ca0)

It’s partly done.

I still need to test the case where no values (humidity) are available for a given room.

How should I do this?

I tried adding a room + a device + device feature (without values) in the seed, but it messes up the other tests (room/device, etc.). So I think this isn’t the right method.

If I use the only room available in the tests (kitchen), the test fails because a value is returned (which is normal).

I prefer to ask even if it’s really basic because I’m stuck.

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [March 12, 2021, 11:46am UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/2 "2021-03-12T11:46:23Z")

</div>

You can create a room just in your test simply 🙂 When it’s just for a single test, it’s better to avoid creating global seeds

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [March 12, 2021, 1:13pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/3 "2021-03-12T13:13:43Z")

</div>

Ok, I’ve done this: probably messy but well

```js
it('should ask the humidity in a room with no values', async () => {
    const brain = {
      addRoom: fake.returns(null),
      removeRoom: fake.returns(null),
    };
    const stateManager = new StateManager(event);
    const deviceManager = new Device(event, messageManager, stateManager, {});
    const serviceManager = new ServiceManager({}, stateManager);
    const room = new Room(brain);
    await room.create('test-house', {
      name: 'No value Room',
    });
    const roomFound = await room.getBySelector('no-value-room');
    const device = new Device(event, {}, stateManager, serviceManager);
    await device.create({
      id: '7bbe2ed8-e7c7-4b89-8421-b52fea5491ef',
      name: 'HUMIDITY DEVICE',
      selector: 'humidity-device',
      external_id: 'humidity-device-external',
      service_id: 'a810b8db-6d04-4697-bed3-c4b72c996279',
      room_id: roomFound.id,
      created_at: '2021-02-12 07:49:07.556 +00:00',
      updated_at: '2021-02-12 07:49:07.556 +00:00',
      features: [
        {
          id: '984d23b0-3508-4d9e-ba91-63cfc3f9e536',
          name: 'Test humidity sensor null',
          selector: 'test-humidity-sensor-null',
          external_id: 'humidity-sensor:2',
          category: 'humidity-sensor',
          type: 'humidity',
          unit: 'percent',
          read_only: false,
          has_feedback: false,
          min: 0,
          max: 100,
          last_value: null,
          last_value_changed: new Date().toISOString(),
          device_id: '7bbe2ed8-e7c7-4b89-8421-b52fea5491ef',
        },
      ],
    });
    const message = {};
    await deviceManager.humiditySensorManager.command(
      message,
      {
        intent: 'humidity-sensor.get-in-room',
        entities: [
          {
            sourceText: 'No value Room',
            entity: 'room',
          },
        ],
      },
      {
        room: roomFound.id,
      },
    );
    assert.calledWith(messageManager.replyByIntent, message, 'humidity-sensor.get-in-room.fail.no-results', {
      room: roomFound.id,
      roomName: 'No value Room',
      humidity: null,
      unit: 'percent',
    });
  });

```

I think I’m close

The log:

```auto
2021-03-12T14:00:11+0100 <debug> device.notify.js:20 (DeviceManager.notify) Notify device humidity-device creation
2021-03-12T14:00:11+0100 <warn> device.notify.js:33 (DeviceManager.notify) Service a810b8db-6d04-4697-bed3-c4b72c996279 was not found.
2021-03-12T14:00:11+0100 <debug> humidity-sensor.getHumidityInRoom.js:21 (HumiditySensorManager.getHumidityInRoom) Getting average humidity in room de14c6bf-d739-479f-a3f2-85ab783b68ed
2021-03-12T14:00:11+0100 <debug> humidity-sensor.command.js:37 (HumiditySensorManager.command) NoValuesFoundError: No humidity values found in this room.
    at HumiditySensorManager.command (/home/vonox/repos/GladysFork/server/lib/device/humidity-sensor/humidity-sensor.command.js:26:17)
    at async Context.<anonymous> (/home/vonox/repos/GladysFork/server/test/lib/device/humidity-sensor/humidity-sensor.test.js:113:5)
    1) should ask the humidity in a room with no values

```

The result is there =\> `NoValuesFoundError: No humidity values found in this room.`

But

 ![image](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/d/d25e3fc7a0678d36370dfa51110fe89e86719ef0.png)

Again this context issue I thought I understood, how to read this output?

EDIT: in green what is expected and in red what was produced/received?

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [March 13, 2021, 12:39am UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/4 "2021-03-13T00:39:06Z")

</div>

It’s not dirty! On the contrary, I think it’s better to create your own little scenario in your test, I like that 🙂

> [@VonOx](#):
>
> EDIT: green for what is expected and red for what was produced/received?

Exactly, that’s it.

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [March 13, 2021, 7:50am UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/5 "2021-03-13T07:50:28Z")

</div>

Okay, I was afraid I had understood :D, so I don’t understand what the kitchen has to do with it ^^

---

<div class="post-metadata">

**Author:** ![AlexTrovato](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/alextrovato/32/10705_2.png) [@AlexTrovato](https://community.gladysassistant.com/u/AlexTrovato)\
**Post date:** [March 13, 2021, 8:56am UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/6 "2021-03-13T08:56:28Z")

</div>

lol who would have thought, dev looks like a real job?? 😛

Do you want to pair up?

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [March 13, 2021, 9:37am UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/7 "2021-03-13T09:37:09Z")

</div>

> [@AlexTrovato](#):
>
> Does dev look like a real job??

We’re not going that far 🤣

> [@AlexTrovato](#):
>
> Do you want to pair up?

Not necessarily (you’ve got other fish to fry).

What I really don’t want is to have a turnkey solution without understanding.

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [March 13, 2021, 2:51pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/8 "2021-03-13T14:51:58Z")

</div>

After several attempts, I still don’t understand why the code is returning information from another Room.

I’ve added logging everywhere to test the test ^^!

By the way, it’s really strange because the red output is from the first test.

---

<div class="post-metadata">

**Author:** ![AlexTrovato](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/alextrovato/32/10705_2.png) [@AlexTrovato](https://community.gladysassistant.com/u/AlexTrovato)\
**Post date:** [March 13, 2021, 3:18pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/9 "2021-03-13T15:18:32Z")

</div>

I’m taking your branch locally to test, I tell you.

---

<div class="post-metadata">

**Author:** ![AlexTrovato](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/alextrovato/32/10705_2.png) [@AlexTrovato](https://community.gladysassistant.com/u/AlexTrovato)\
**Post date:** [March 13, 2021, 3:26pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/10 "2021-03-13T15:26:32Z")

</div>

@VonOx

I recommend adding the ability to run the test file in debug mode in VSCode and to be able to add breakpoints to see what happens throughout the process.

 ![image](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/9/9244be6dfe5d5094a1dd4ba2ef2d4c58720b379c.png)

You add in the file

```auto
    {
      "type": "node",
      "request": "launch",
      "name": "NodeJS test current file",
      "runtimeArgs": [
        "${workspaceFolder}/server/node_modules/mocha/bin/mocha",
        "--timeout",
        "999999",
        "--colors",
        "${workspaceFolder}/server/test/bootstrap.test.js",
        "${file}",
        "--exit"
      ],
      "console": "integratedTerminal",
      "env": {
        "NODE_ENV": "test",
        "SQLITE_FILE_PATH": "/tmp/gladys-test.db"
      }
    },

```

Then, the little green arrow to run the open file in debug mode, having the breakpoints… cough cough

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [March 13, 2021, 3:26pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/11 "2021-03-13T15:26:50Z")

</div>

I didn’t push the test snippet I put above (thanks 🙂 )

---

<div class="post-metadata">

**Author:** ![AlexTrovato](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/alextrovato/32/10705_2.png) [@AlexTrovato](https://community.gladysassistant.com/u/AlexTrovato)\
**Post date:** [March 13, 2021, 3:27pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/12 "2021-03-13T15:27:41Z")

</div>

Yes, I’ve retrieved it, I have the error.

---

<div class="post-metadata">

**Author:** ![AlexTrovato](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/alextrovato/32/10705_2.png) [@AlexTrovato](https://community.gladysassistant.com/u/AlexTrovato)\
**Post date:** [March 13, 2021, 3:36pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/13 "2021-03-13T15:36:35Z")

</div>

Just before continuing, when you use the «&nbsp;fake&nbsp;» of sinon,  
you need to remember to reset sinon with

```javascript
  afterEach(() => {
    sinon.reset();
  });

```

---

<div class="post-metadata">

**Author:** ![AlexTrovato](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/alextrovato/32/10705_2.png) [@AlexTrovato](https://community.gladysassistant.com/u/AlexTrovato)\
**Post date:** [March 13, 2021, 3:43pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/14 "2021-03-13T15:43:57Z")

</div>

In the `command` function you created, you raise a `NoValuesFoundError` if your device has no value (test case above).

But the `context` is «&nbsp;filled&nbsp;» after the error.

The context is therefore

```json
    {
      room: roomFound.id,
    }

```

without anything else around it.

Your test is not bad, but there is a gap between what you do in `command` and the result you expect.

On line 37, add the «&nbsp;context&nbsp;» to your debug, you’ll see 😉

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [March 13, 2021, 4:36pm UTC](https://community.gladysassistant.com/t/help-tests-unitaire/6170/15 "2021-03-13T16:36:14Z")

</div>

Got it, thanks Alex, I removed the device creation, it was useless 🙂
