Skip to content

Adding Captors#1

Merged
lucailmartini merged 8 commits intolucailmartini:captorsfrom
typemoq:master
Sep 3, 2020
Merged

Adding Captors#1
lucailmartini merged 8 commits intolucailmartini:captorsfrom
typemoq:master

Conversation

@lucailmartini
Copy link
Owner

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 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

@lucailmartini lucailmartini merged commit 62b84d9 into lucailmartini:captors Sep 3, 2020
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.

3 participants