Reputation: 15
I have a method in my controller.
public JsonResult GetMemberSubgroupSpecifications(int rows, int page)
{
List<VersionSpecification> versionSpecifications = _sessionAccessor.Contract.VersionSpecifications
.Where(x => x.SpecificationType.Id == (int)SpecificationTypes.MemberSubGroup).ToList();
List<MemberSubGroup> subGroups = _sessionAccessor.MemberSubGroups;
if (subGroups == null)
{
subGroups = _subGroups.GetMemberSubGroups();
}
else if (subGroups.Count == 0)
{
subGroups = _subGroups.GetMemberSubGroups();
}
var specificationValues = versionSpecifications.GroupBy(x => x.Value).ToDictionary(r => r.Key, z => z.First());
List<MemberSubGroup> contractMemberSubGroups = subGroups.Where(x => specificationValues.ContainsKey(x.Id)).ToList();
_sessionAccessor.MemberSubGroups = contractMemberSubGroups;
var jsonData = contractMemberSubGroups.Select(x => new
{
Id = x.Id,
MemberSubGroupName = x.SubGroupName,
MemberGroupName = x.MemberGroup.GroupName,
MemberGroupId = x.MemberGroup.Id,
SubGroupCode = x.SubGroupCode,
StartDate = specificationValues[x.Id].StartDate,
EndDate = specificationValues[x.Id].EndDate
}).ToList();
var json = _jsonHelper.FormatContractDetailsJsonData(rows, page, jsonData);
return json;
When trying to write a unit test for this method, I can't get
_jsonHelper.FormatContractDetailsJsonData(rows, page, jsonData);
to return anything but null.
Here's my unit test for the method.
[Test]
public void GetMemberSubGroupsSpecifications_ReturnsJson()
{
//Arrange
var jsonHelper = MockRepository.GenerateMock<IJsonHelper>();
jsonHelper.Stub(x => x.FormatContractDetailsJsonData(Arg<int>.Is.Anything, Arg<int>.Is.Anything, Arg<List<object>>.Is.Anything)).Return(new JsonResult() { });
var specificationType = MockRepository.GenerateMock<SpecificationType>();
specificationType.Id = 2;
var versionSpecification = MockRepository.GenerateMock<VersionSpecification>();
versionSpecification.SpecificationType = specificationType;
versionSpecification.Value = 2;
var sessionAccessor = MockRepository.GenerateMock<ISessionAccessor>();
var memberGroup = MockRepository.GenerateMock<MemberGroup>();
memberGroup.GroupName = "groupName";
memberGroup.Id = 2;
var memberGroups = new List<MemberGroup>()
{
memberGroup
};
var contractVersion = MockRepository.GenerateMock<ContractVersion>();
contractVersion.VersionSpecifications = new List<VersionSpecification>()
{
versionSpecification
};
var memberSubGroup = MockRepository.GenerateMock<MemberSubGroup>();
memberSubGroup.Id = 2;
memberSubGroup.MemberGroup = memberGroup;
memberSubGroup.SubGroupCode = "subGroupCode";
var listMemberSubGroups = new List<MemberSubGroup>()
{
memberSubGroup
};
sessionAccessor.Stub(x => x.MemberGroups).Return(memberGroups);
sessionAccessor.Stub(x => x.Contract).Return(contractVersion);
sessionAccessor.Stub(x => x.MemberSubGroups).Return(listMemberSubGroups);
var searchController = ObjectFactory.GetSearchController(sessionAccessor, null, null, jsonHelper);
//Act
var actual = searchController.GetMemberSubgroupSpecifications(1, 1);
//Assert
Assert.IsNotNull(actual);
Assert.IsInstanceOf(typeof(JsonResult), actual);
}
I've been struggling with this for a few days now and I can't find any thing that would help me in any of my searches. Also, I'm still fairly new to using rhino mocks and unit testing in general.
Here is the FormatContractDetailsToJson method:
public JsonResult FormatContractDetailsJsonData<T>(int rows, int page, List<T> data)
{
var totalPages = (int)Math.Ceiling(data.Count / (float)rows);
JsonResult json = new JsonResult();
json.Data = new
{
total = totalPages,
page,
records = data.Count,
rows = data
};
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return json;
}
I believe the list data parameter is the reason why this is happening, but I'm not sure.
Upvotes: 1
Views: 661
Reputation: 31464
You got types mismatch in your setup code:
jsonHelper.Stub(x => x.FormatContractDetailsJsonData(
Arg<int>.Is.Anything,
Arg<int>.Is.Anything,
Arg<List<object>>.Is.Anything))
.Return(new JsonResult() { });
In your tested code 3rd parameter won't be a list of objects - it will be list of compiler generated anonymous type instances against which you are not making any setups.
In other words, your tests make setup to:
FormatContractDetailsJsonData<object>
While your implementation calls more or less:
FormatContractDetailsJsonData<<>f_AnonymousType...>
There is a hack however. If your tests live in the same assembly as your tested code you can generate dummy of such type by simply declaring yet another anonymous type with properties with the same names, types and order:
var dummyList = Enumerable
.Range(0, 1)
.Select(x => new
{
Id = 1,
MemberSubGroupName = "",
MemberGroupName = "",
MemberGroupId = 1,
SubGroupCode = "",
StartDate = DateTime.Now,
EndDate = DateTime.Now,
})
.ToList();
And set up your mock like this:
jsonHelper.Stub(x => x.FormatContractDetailsJsonData(1, 1, dummyList))
.IgnoreArguments()
.Return(new JsonResult() { });
Unfortunately, if this is not the case (anonymous types originating from the same assembly), you'll most likely have to create separate type for jsonData
to make your matching work.
Upvotes: 1