Skip to content

constructedWith<>() does not work #1355

@TWiStErRob

Description

@TWiStErRob

There's documentation of anyConstructed<>() and constructedWith<>(...) at https://mockk.io/#constructor-mocks

If you copy those code snippets and execute them anyConstructed works fine, but constructedWith:

  1. The example does not compile to begin with (fixed here)
  2. After fixing the error the test just fails
        assertEquals(2, MockCls().add(1)) // FAIL: expected 2, actual 1

Apparently the mocking has no effect.

Here's an isolated full example (removed the more advanced lines from the docs):

    @Test fun test() {
        class MockCls(private val a: Int = 0) {
            fun add(b: Int) = a + b
        }

        mockkConstructor(MockCls::class)

        every { constructedWith<MockCls>().add(1) } returns 2

        assertEquals(2, MockCls().add(1)) // FAIL: expected 2, actual 1

        verify {
            constructedWith<MockCls>().add(1)
        }
    }

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions