Home > Net >  FakeItEasy A.CollectionOfFake doesn't work
FakeItEasy A.CollectionOfFake doesn't work

Time:10-13

I have a simple WCF service that returns a list of PackagesModel.UnitTypeList to my ASP.NET MVC 4 controller

[HttpGet]
public ContentResult GetUnitType() 
{
    List<PackageModelUnitTypeList> _result = lms_client.GetUnitType().ToList();
    return cmn.SerializeToJSON(_result);
}

Here's my test that fakes the return list of my service to the controller.

[TestMethod]
public void should_GetUnitType()
{
    // Arrange
    int expected_status_code = (int)HttpStatusCode.OK;
    int expected_list_count = 5;

    var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), _controller_Packages);
    _controller_Packages.ControllerContext = context;

    // Act
    A.CallTo(() => _lms_service.GetUnitType())
        .Returns(new PackageModelUnitTypeList[]{
            new PackageModelUnitTypeList(),
            new PackageModelUnitTypeList(),
            new PackageModelUnitTypeList(),
            new PackageModelUnitTypeList(),
            new PackageModelUnitTypeList()
        });

    var _getUnitType = _controller_Packages.GetUnitType() as ContentResult;
    List<PackageModelUnitTypeList> _result = JsonConvert.DeserializeObject<List<PackageModelUnitTypeList>>(_getUnitType.Content);

    // Assert
    Assert.AreEqual(expected_list_count, _result.Count);
}

I am interested on FakeItEasy's enter image description here

Below is my implementation:

A.CallTo(() => _lms_service.GetUnitType())
                .Returns(A.CollectionOfFake<PackageModelUnitTypeList>(5));

Can someone please help identify why this happens and how to make this work? Any help would be appreciated. Thanks.

UPDATE

Based on @Blair Conrad's answer, this is what it looks like. I debugged the tests and it indeed returns faked list based on the number I wanted:

A.CallTo(() => _lms_service.GetUnitType())
                .Returns(Enumerable.ToArray(A.CollectionOfFake<PackageModelUnitTypeList>(1000)));

This is what I need. Thanks for the help!

CodePudding user response:

It looks like _lms_service.GetUnitType() returns a PackageModelUnitTypeList[]. A.CollectionOfFake<PackageModelUnitTypeList> returns an IList<PackageModelUnitTypeList>. You could convert by calling Enumerable.ToArray, likely.

  • Related