tags:

views:

34

answers:

1

Benn trying to test a service layer method which returns a simple IList using RhinoMocks 3.6

[TestMethod]
    public void GetItems_Returns_ActionItemsList()
    {
        // arrange
        var mockRepository = MockRepository.GenerateMock<IActionItemRepository>();
        mockRepository.Stub(s => s.Select()).Return(GetFakeActionItems());

        var service = new ActionItemQueueService(mockRepository);

        // act
        var actual = service.GetItems();

        mockRepository.VerifyAllExpectations();

        // assert
        Assert.IsInstanceOfType(actual, typeof(IList<ActionItem>));
    }

Real simplle right? The GetFakeActionItems method for reference;

internal IQueryable<ActionItem> GetFakeActionItems()
    {
        return new List<ActionItem> {            
                                        new ActionItem{
                                            Id = 5,
                                            ClientName = "Bank of American Corporation",
                                            ActionNeeded = RequiredAction.Change,
                                            RecordIndicator = "ROI",
                                            RequestDate = DateTime.Now.AddDays(-3)
                                        } 
        } as IQueryable<ActionItem>;
    }

Here is the class and method under test...

public class ActionItemQueueService : IActionQueueService
{
    private readonly IActionItemRepository _actionItemRepository;

    public ActionItemQueueService(IActionItemRepository actionItemRepository)
    {
        _actionItemRepository = actionItemRepository;
        if (_actionItemRepository == null)
        {
            throw new ArgumentNullException("ActionItemRepository");
        }
    }




    public IList<Model.ActionItem> GetItems()
    {
        return _actionItemRepository.Select().ToList<Model.ActionItem>();
    }
}

Nothing wild.... When I run my test, I Get an ArgumentNullException when the GetItems() method is called when _actionItemRepository.Select() fires. Value cannot be null. Parameter name: source.

Confused and perplexed wondering if anyone has any insight into what I am missing.

Many thanks!

+2  A: 

I believe the problem is that you are taking your List<ActionItem> and performing "as IQueryable<ActionItem>" on it in your GetFakeActionItems method. This returns null as it is not a valid conversion.

Thus, when your mock ActionItemRepository runs the stubbed out Select() it returns null (via GetFakeActionItems), which causes the subsequent ToList<Model.ActionItem>() to go down in flames with the ArgumentNullException.

What you actually want to do in your GetFakeActionItems is:

myList.AsQueryable()
Andrew Anderson
Yep. That was the issue... Many thanks Adrew. Making the change allowed the test to pass as expected.
Jeff Spicoli