Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query parameters verification and unmatched params #54

Open
Tunous opened this issue Apr 2, 2018 · 5 comments
Open

Query parameters verification and unmatched params #54

Tunous opened this issue Apr 2, 2018 · 5 comments

Comments

@Tunous
Copy link

Tunous commented Apr 2, 2018

Perhaps I'm not understanding correctly how query parameters should be tested but right now the usage of their verification seems to be working in an unexpected way to me. I assumed that it would work as an exact match, where it would check for each query parameter and result in an error whenever one is missing or there are some unmatched parameters.

For example, if I verified a call like this

server.verify("path").called(
    queryParams = params(
        "param1" to "value1",
        "param2" to "value2"
    )
)

Then I would expect it to be successful for /path?param1=value1&param2=value2 (which works correctly) and fail for /path?param1=value1 (also works correctly) or for /path?param1=value1&param2=value2&param3=value3 (which is not the case). Is there something that I'm missing which would make it ok to not fail when there is incorrect number of params? And if there is, then is there a way to make these cases result in failure?

@JorgeCastilloPrz
Copy link
Owner

JorgeCastilloPrz commented Apr 3, 2018

Could you paste here a couple of tests here where you think it's not doing what you would expect? That would be helpful for me to understand the problem and test it by myself. Thanks in advance.

@JorgeCastilloPrz
Copy link
Owner

JorgeCastilloPrz commented Apr 3, 2018

These 2 tests are passing and I'm expecting an assertion error, so when you expect more params than the ones sent it's actually reporting an error. Are you sure about your second statement? Also pls let me know if you're using version 0.0.7

@Test(expected = AssertionError::class)
    fun reportsErrorWhenMissingExpectedQueryParamWithParamList() {
        server.whenever(Method.POST, "my-fake-service/edit-tag")
                .thenRespond(success())

        service.getNewsByIds(listOf("1", "2")).execute()

        server.verify("my-fake-service/edit-tag").called(
                times = once(),
                queryParams = params(
                        "id" to "1",
                        "id" to "2",
                        "id" to "3"))
    }

    @Test(expected = AssertionError::class)
    fun reportsErrorWhenMissingExpectedQueryParam() {
        server.whenever(Method.POST, "my-fake-service/edit-tag")
                .thenRespond(success())

        service.getNew("1").execute()

        server.verify("my-fake-service/edit-tag").called(
                times = once(),
                queryParams = params(
                        "id" to "1",
                        "id" to "2"))
    }

@Tunous
Copy link
Author

Tunous commented Apr 3, 2018

Sorry if I wasn't clear enough. I meant that test for when I expect less params than the one sent should fail. Tests when I expect more params indeed report an error.

Here is an example test based on your where I expect fewer params than returned:

@Test(expected = AssertionError::class)
fun reportsErrorWhenMissingExpectedQueryParam() {
    server.whenever(Method.POST, "my-fake-service/edit-tag")
            .thenRespond(success())

    service.getNewsByIds(listOf("1", "2")).execute()

    server.verify("my-fake-service/edit-tag").called(
            times = once(),
            queryParams = params("id" to "1")
    )
}

Edit: And yes I tested it using version 0.0.7.

@JorgeCastilloPrz
Copy link
Owner

JorgeCastilloPrz commented Apr 3, 2018

Ok. This has been done intentionally like this, but it's open to discussion. The fact is that it's still true that endpoint "my-fake-service/edit-tag" is being called using the query param "id" to "1", which is what you intentionally wanted to assert. It remains true even if incomplete. That leaves the door open to users to just assert over some important params being sent and ignore the resting ones if they don't really care about those being sent or not. But that might also be prone to errors. So you might be true on the need to also report an error if you don't assert over all of them :/

Do you have any clue on what other libs like WireMock or RestMock do for this given scenario?

@Tunous
Copy link
Author

Tunous commented Apr 4, 2018

Do you have any clue on what other libs like WireMock or RestMock do for this given scenario?

Unfortunately not. I'm new to this form of testing and your library is the first one I've decided to experiment with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants