# Comparing a method with an array using "contain.oneOf"

**URL:** <https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [October 14, 2023, 4:29pm UTC](https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285 "2023-10-14T16:29:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![satellite-geologis10](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@satellite-geologis10](https://community.postman.com/u/satellite-geologis10)\
**Post date:** [October 14, 2023, 4:29pm UTC](https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285/1 "2023-10-14T16:29:36Z")

</div>

I need to check if the current request method is in an array with other methods. This test works:

```auto
if ((pm.request.method === "GET") || (pm.request.method === "HEAD")){
    pm.test("Verify that the GET and HEAD methods are not deactivated", function () {
        pm.expect(pm.response.code).to.not.eql(405);
});
}

```

I tried to write it differently, but **this code doesn’t work** :

```auto
if (pm.expect(pm.request.method).to.contain.oneOf(['GET', 'HEAD'])) {
    pm.test("Verify that the GET and HEAD methods are not deactivated", function () {
        pm.expect(pm.response.code).to.not.eql(405);
});
}

```

The error:

> There was an error in evaluating the test script: AssertionError: expected ‘POST’ to contain one of [‘GET’, ‘HEAD’]

What must be done to make the second condition (if) to work?

---

<div class="post-metadata">

**Author:** ![xk0der](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/xk0der/32/2056_2.png) [@xk0der](https://community.postman.com/u/xk0der)\
**Post date:** [October 15, 2023, 8:26pm UTC](https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285/2 "2023-10-15T20:26:57Z")

</div>

Hey @satellite-geologis10 ,

You may want to look at the [lodash](https://lodash.com/) library available within [Postman’s Sandbox](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries).

Assertions ideally should be within `pm.test()`'s, so that the script execution does not stop. In your case, since the current request’s HTTP method is `POST`, your `pm.expect()` statement fails with the error message you get, which is the correct behaviour.

Using lodash’s [includes](https://lodash.com/docs/4.17.15#includes) method, you can accomplish what you are after, as follows:

```auto
if (_.includes(['GET', 'HEAD'], pm.request.method)) {
    pm.test("Verify that the GET and HEAD methods are not deactivated", function () {
        pm.expect(pm.response.code).to.not.eql(405);
    });
}

```

---

<div class="post-metadata">

**Author:** ![satellite-geologis10](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@satellite-geologis10](https://community.postman.com/u/satellite-geologis10)\
**Post date:** [October 16, 2023, 10:57am UTC](https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285/3 "2023-10-16T10:57:35Z")

</div>

Works perfect! So Postman allows you to use syntax from both the Chai and Lodash libraries? Are there other resources where I can get other references to operations/statements?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 16, 2023, 11:41am UTC](https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285/4 "2023-10-16T11:41:43Z")

</div>

The details on the external libraries are here.

[Using external libraries | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 16, 2023, 11:53am UTC](https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285/5 "2023-10-16T11:53:30Z")

</div>

On a side note, you can use the includes() method for arrays.

```auto
if (['GET', 'HEAD'].includes(pm.request.method)) {
    pm.test("Verify that the GET and HEAD methods are not deactivated", function () {
        pm.expect(pm.response.code).to.not.eql(405);
    });
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 19, 2023, 11:53am UTC](https://community.postman.com/t/comparing-a-method-with-an-array-using-contain-oneof/53285/6 "2023-10-19T11:53:48Z")

</div>

Closed on October 19, 2023, 11:53am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
