Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Return Null value from method using FakeItEasy

I have a service faked using FakeitEasy and i am trying to call its method. Here is the Code

        var client = container.Resolve<MyService>();
        A.CallTo(() => client.GetUserProfile(userName)).Returns(null);

The method GetUserProfile returns some object in actual implementation. but for some reason i want this method to return null. I am using above code to acomplish this purpose but its returning Fake object instead of null.

Here is the Test Setup i am using

    [Test]
    public void MyTest(string sitecollectionGuid, string customerName)
    {
        var mockHttpContext = SetupHttpContext(sitecollectionGuid, customerName);

        var client = container.Resolve<MyService>();
        A.CallTo(() => client.GetUserProfile(userName)).Returns(null);

        var controllerContext = new ControllerContext(mockHttpContext, new RouteData(), A.Fake<ControllerBase>());
        controller.ControllerContext = controllerContext;

        var result = controller.CheckUsername(userName);
        Assert.IsNotNull(result, "Result is not as expected");
    }

Production Method looks like the following

public UserDAO GetUserProfile(string userName)
    {
        UserDAO objUserProfile = new UserDAO();
        IUsers objUsers = (IUsers)Global.Container["Users"];
        IUser objUser = objUsers.GetByUserName(userName);
        if (objUser == null)
        {

            return null;
        }
        else
        {
            objUserProfile = AutoMapper.Mapper.Map<IUser, UserDAO>(objUser);
            objUserProfile.FirstName = objUser.FirstName;
            objUserProfile.MiddleName = objUser.MiddleName;
            objUserProfile.LastName = objUser.LastName;
            ....................
            ....................
            <setting other properties>
            .................... 
            ....................

            return objUserProfile;
        }
    }

Any help will be appreciated

Thanks

like image 574
Ranjit Avatar asked Sep 05 '14 09:09

Ranjit


2 Answers

Try and type your (null) reference.

UserDAO returnValue = null;    
var client = container.Resolve<MyService>();
        A.CallTo(() => client.GetUserProfile(userName)).Returns(returnValue);
like image 102
Andreas Lundgren Avatar answered Sep 21 '22 20:09

Andreas Lundgren


In order to configure a method, it has to be virtual, abstract, or defined on an interface that you're faking. However,

public UserDAO GetUserProfile(string userName)

is neither virtual nor abstract, so unless you're creating a fake from an interface, this will not work. However, A.CallTo will raise an error when trying to configure either a non-virtual method or a method on a concrete (not faked) object, and you've not mentioned either of these things happening.

From your code, we still can't tell

  1. where client came from (I know, the container, but how did it get there?),
  2. whether controller uses the same client, and
  3. what the connection between controller.CheckUsername and client.GetUserProfile is

My guesses at this point are

  1. whatever controller is using to CheckUsername, it's not the same client that the test has, or
  2. client.GetUserProfile is being called with the wrong userName (although you use the same one in controller.CheckUsername(userName), so that seems less likely)

If you're unable or unwilling to connect the dots, I suggest checking the value of userName at all points, and making sure that when client is called in the production code, it's a faked object (debug in and examine the type—it should be clear whether its your type or the faked one).

like image 41
Blair Conrad Avatar answered Sep 20 '22 20:09

Blair Conrad