0

I am quite new to Moq and I am not sure is this even possible, but let say, if I have one method named GeneratedHashedAndSaltedPassword()and I am making a call to that method from method named CheckUsersPassword() is there any chance that i can write from Unit test Moq expression that says: Verify if GeneratedHashedAndSaltedPassword is call from CheckUsersPassword method?

I will know that CheckUsersPassword method preformed correctly if it makes a call to GeneratedHashedAndSaltedPassword method without any exception.

What I have for now is not much, since I am having difficult time with Moq and it's logic.

[Test] public void CheckIfPasswordIsHashed_Test() { var securityServiceMock = new Mock<User>(); securityServiceMock.Setup(m => m.CheckUsersPassword(It.IsAny<string>())).Returns(true); securityServiceMock.VerifyAll(); } 

I was trying to SetUp CheckUsersPassword, but I still don't know how or even if I can preform Verification I need. Does somebody has more experience with this kind of issue?

This is what CheckUserPassword does:

public bool CheckUsersPassword(string password) { return SecurityService.GenerateHashedAndSaltedPassword(password, Salt) == Password; } 

It makes simple call to GenerateHashedAndSaltedPassword() and compares result with current user password.

2
  • Is SecurityService instance injected into the constructor of the class that contains the CheckUsersPassword method? Commented Sep 14, 2016 at 13:44
  • sure looks like a public static method to me. Commented Sep 14, 2016 at 17:12

1 Answer 1

1

Important is to distinguish between code which is tested with unit test and external dependencies of this code. External dependencies are mocked with MOQ but tested code must be instantiated and called in the unit test directly.

In your question I assume you test the method CheckUsersPassword and that SecurityService is a dependency which should be mocked.

In the test you have to create instance of class (which contains the tested method) and pass mock of dependency to this tested class somehow.

[Test] public void CheckIfPasswordIsHashed_Test() { // Arrange // Mock dependency which is the service and setup // GenerateHashedAndSaltedPassword so it does what you need var securityServiceMock = new Mock<User>(); securityServiceMock.Setup(m => m.GenerateHashedAndSaltedPassword( It.IsAny<string>(), ItIsAny<Salt>()) .Returns(Password); // TestedClass needs a place where the mocked instance can be injected, // e.g. in constructor, or public property var testedClass = new TestedClass(securityServiceMock.Object); // Act testedClass.CheckUsersPassword(password); // Assert securityServiceMock.Verify(m => m.GenerateHashedAndSaltedPassword( It.IsAny<string>(), ItIsAny<Salt>()), Times.Once()); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. In meantime I've realized that external dependencies in unit that is tested must be mocked with Moq, but tested code must be real instance :) But it is great that we have such a good explanation here :) @dee

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.