Tester Shah
Tester Shah

Reputation: 13

How to write unit test for this block of code

public static LoginResult CreateLoginSuccessResponse(this PortalIdentity user)
    {
        return new LoginResult(
            true,
            string.Empty,
            user.Roles,
            false
        );
    }

public class PortalIdentity : IdentityUser
{
    public string Firstname { get; set; }

    public string Lastname { get; set; }
}

This block of code is for creating a success response whenever a user logs in successfully. I am a QA and trying to learn how to write unit tests. I am not sure how to write unit test for "this" keyword

Upvotes: 0

Views: 223

Answers (1)

mjwills
mjwills

Reputation: 23975

this (in the above context at least) just means it is an extension method.

To test it, new up a PortalIdentity, assign to a variable (bob) and call bob.CreateLoginSuccessResponse():

var bob = new PortalIdentity(you may need parameters here);
var result = bob.CreateLoginSuccessResponse();

If the call to bob.CreateLoginSuccessResponse() doesn't compile, then the extension method is likely in a different namespace. As per the docs, you need to:

In the calling code, add a using directive to specify the namespace that contains the extension method class.

Upvotes: 1

Related Questions