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

Verifying a method call from within a Promise reject block #105

Open
calebdinsmore opened this issue Jul 10, 2018 · 1 comment
Open

Verifying a method call from within a Promise reject block #105

calebdinsmore opened this issue Jul 10, 2018 · 1 comment

Comments

@calebdinsmore
Copy link

I've been going at this for a while now and can't seem to figure out why it's not working or if it's even possible.

The framework being used is AngularJS.

Here's the test code:

    describe('function login', () => {
        it('should display toast notification when login fails', () => {
            vm.username = '';
            vm.password = '';
            MockLoginService.setup(l => l.login(It.isAnyString(), It.isAnyString())).returns(() => Promise.reject({ data: '' }) as any);

            vm.login({});

            MockNotificationService.verify(n => n.error(It.isAnyString()), Times.once());
        });
    });

Here's the method under test:

    login($event: any) {
        this.isLoading = true;
        this.LoginService.login(this.username, this.password).then(response => {
            // removed unnecessary code here for issue
        }, response => {
            this.isLoading = false;
            if (response.data == 'PasswordExpired') {
                this.NotificationService.error('Your password has expired.');
                this.forgotPassword($event, true);
            } else {
                this.NotificationService.error(`Failed to log in: ${response.data}`);
            }
        }, () => this.isLoading = false);
    }

The line that should be called (and that I know is being called during testing thanks to console statements) is this.NotificationService.error(Failed to log in: ${response.data});

Even though I'm 100% certain that the .error method is being called at runtime, the test runner is saying it never gets invoked. I believe that my test is written correctly, since if I copy that line and pull it out of the Promise block (e.g. at the top of the login function's body), the test passes.

Is it not possible in TypeMoq to verify that a mock's method was called within a Promise/lambda function?

Thanks for any help/clarity!

@Diemauerdk
Copy link

Hi @cjdinsmore and @florinn ,

I am in the exact same situation - just in Angular 6. Looking forward for an answer :)

/Diemauerdk

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