Authentication of the device under test

I am new to unit testing. I am creating an ASP.NET MVC3 application (although my question seems to be a language agnostic) and I am confused in the basic test.

I want to do a unit test to make sure that my ValidatePassword function works - it will contain a username and password, then a hash password and see if it matches the hash for the user in the database. If so, it returns true. The problem is that I am using a mock repository, so I will need to add a user to db before running my test. I can’t create this user in my test setup because I don’t know what will be the encrypted password until I run it through the function that I am testing. Is the answer to run it through the Hash function once, write it in my test, and then check with that?

I hope this is clear. Thank!

+3
source share
2 answers

, , , , , . . ...

, , . :

AddNewUser("username", "passsword");
bool isValid = ValidateUser("username", "password");
Assert.IsTrue(isValid);

, , /:

test: ValidUser_InvalidPassword:
    AddNewUser("username2", "pwd");
    bool isValid = ValidateUser("username2", "wrongPassword");
    Assert.IsFalse(isValid);


test: NonExistingUser:
    bool isValid = ValidateUser("non_existing_user", "anyPassword");
    Assert.IsFalse(isValid);

, . , . ?

- .. , , , . .

: , -, . , ( / ), , .

Dr Dobbs:

+5

, mock-. , .

+2

All Articles