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

Adding Captor and its test #122

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

lucailmartini
Copy link

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

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