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
Try and type your (null) reference.
UserDAO returnValue = null;
var client = container.Resolve<MyService>();
A.CallTo(() => client.GetUserProfile(userName)).Returns(returnValue);
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
client
came from (I know, the container, but how did it get there?),controller
uses the same client
, andcontroller.CheckUsername
and client.GetUserProfile
isMy guesses at this point are
controller
is using to CheckUsername
, it's not the same client
that the test has, orclient.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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With