3

I have a rather simple problem . I am simply trying to test asp.net Identity's UserStore method and have this in my test. Essentially the goal was simply to create a mock user store( in memory), insert a dummy user and verify that the insert succeeded.

[Test] public void Can_Create_User() { var identityResult = Task.FromResult(new IdentityResult()); var user = new ApplicationUser() { UserName = "Andy", Email = "[email protected]" }; var store = new Mock<UserStore<ApplicationUser>>(); store.Setup(x => x.CreateAsync(user)) .Returns(identityResult); Assert.IsTrue(identityResult.Result.Succeeded); } 

But the test keeps failing with 'Expected true' error.

Thank you

2 Answers 2

4

I'm answering my own question as for some reason the question wasn't getting any views and I did manage to fix it .

I don't know if this is the right approach, but the way I fixed it was firstly changing successfulResult to Task<IdentityResult> AND assigning IdentityResult.Success to it

[Test] public void Can_Create_User() { Task<IdentityResult> successfulResult = Task.FromResult<IdentityResult>(IdentityResult.Success); var user = new ApplicationUser() { UserName = "Andy", Email = "[email protected]" }; var store = new Mock<UserStore<ApplicationUser>>(); store.Setup(x => x.CreateAsync(user)).Returns(successfulResult); Task<IdentityResult> tt = (Task<IdentityResult>) store.Object.CreateAsync(user); Assert.IsTrue(tt.Result.Succeeded); } 
Sign up to request clarification or add additional context in comments.

Comments

0

I didn't see your code which you trying to test but as I understand problem is how to mock method which returns IdentityResult?

By default you cannot create new instance of IdentityResult and assign value to Successed property because it doesn't accespt SET, it accept only GET.

Anyway, IdentityResult has protected constructor which accept bool which indicate whether is Successed true or false. It means you can create your own IdentityResultMock class which inherite from IdentityResult and than you can control creation assign Successed.

You can see my example which I answered here.

Please refer to my

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.