Home Ask Login Register

Developers Planet

Your answer is one click away!

ShaneKm February 2016

Moq item from session

How can I moq the following so it returns a new Guid();

private void CheckSomething(){
    user = CustomerContext.Current.User; // returns Guid

CustomerContext is taken from a third party library and it is not injected into my class.

is there any way to do it?


ShaneKm February 2016

I did this.

public class UserContextWrapper : ICustomUserContext
    public Guid UserGuid
        get { return CustomerContext.Current.User; }

Then In my code I had to modify original to:

private void CheckSomething(){
    user = UserContextWrapper.UserGuid ; // returns Guid

Now I'm able to mock. I'm simply injecting ICustomerUserContext into my class and I have mapped UserContextWrapper to ICustomUserContext in IoC Container.

Post Status

Asked in February 2016
Viewed 1,034 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life