Skip to content

Adding Captor and its test#2

Merged
tobysmith568 merged 2 commits intotypemoq:masterfrom
lucailmartini:captors
Oct 11, 2020
Merged

Adding Captor and its test#2
tobysmith568 merged 2 commits intotypemoq:masterfrom
lucailmartini:captors

Conversation

@lucailmartini
Copy link

Hi, I re-create here the pr I made on the typemoq unmaintained repo.
Here it is the the text I wrote for the original pr.

Hi all,
I submit this PR to include Captors to the library. This small contribution is inspired by Mockito's ArgumentCaptor and I find it very useful to test the interaction between view and presenters.

As an example:

type ButtonHandler = () => void;

interface Button {
    name : string
    addClickListener(handler : ButtonHandler) : void
}

interface View {
    onButtonClicked(buttonName : string) : void
}

function linkButton(button : Button, view : View) {
    button.addClickListener(() => view.onButtonClicked(button.name))
}


describe("when a button is linked", () => {
    it ("the view reacts to the click on the button", () => {
        let buttonMock = TypeMoq.Mock.ofType<Button>()
        buttonMock.setup((button) => button.name).returns(() => "name")

        let viewMock = TypeMoq.Mock.ofType<View>()
        let listenerCaptor = TypeMoq.ArgumentCaptor.argumentCaptor<ButtonHandler>()
        linkButton(buttonMock.object, viewMock.object)
        buttonMock.verify((button) => button.addClickListener(listenerCaptor.capture()), Times.once())

        // simulate the click
        listenerCaptor.value()
        viewMock.verify((view) => view.onButtonClicked("name"), Times.once())
    })
})

Please let me know if this addition can be useful.
Best,
Luca

@tobysmith568 tobysmith568 merged commit 19b8c7c into typemoq:master Oct 11, 2020
@tobysmith568
Copy link

Hey @lucailmartini - Sorry I didn't see this sooner!

I've merged this PR, and will push a new version of typemoq-continued soon :)

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

Successfully merging this pull request may close these issues.

2 participants