开发者

ASP.Net MVC TDD using Moq

I am trying to learn TDD/BDD using NUnit and Moq.

The design that I have been following passes a DataService class to my controller to provide access to repositories.

I would like to Mock the DataService class to allow testing of the controllers.

There are lots of examples of mocking a repository passed to the controller but I can't work out how to mock a DataService class in this

scenerio.

Could someone please explain how to implement this?

Here's a sample of the relevant code:

[Test]
public void Can_View_A_Single_Page_Of_Lists()
{
    var dataService = new Mock<DataService>();

    var controller = new ListsController(dataService); 

    ...
}



namespace Services
{
    public class DataService
    {
        private readonly IKeyedRepository<int, FavList> FavListRepository;
        private readonly IUnitOfWork unitOfWork;

        public FavListService FavLists { get; private set; }

        public DataService(IKeyedRepository<int, FavList> FavListRepository,
        IUnitOfWork unitOfWork)
        {
            this.FavListRepository = FavListRepository;
            this.unitOfWork = unitOfWork;

            FavLists = new FavListService(FavListRepository);
    }

        public void Commit()
        {
            unitOfWork.Commit();
        }

    }
}



namespace MyListsWebsite.Controllers
{
    public class ListsController : Controller
    {
        private readonly DataService dataService;

        public ListsController(DataService dataService)
        {
            this.dataService = dataService;
        }


        public ActionResult Index()
        {
            var 开发者_运维技巧myLists = dataService.FavLists.All().ToList();

            return View(myLists);
        }

    }
}


Create an interface like this:

public interface DataService
{
    FavListService FavLists { get; }
    void Commit();
}

Make your DataService implement this interface and your controller should depend on this interface. Problem solved :)

EDIT: This line of code:

dataService.FavLists.All().ToList();

is breaking the law of demeter and will be a pain to unit test your service. Create a method like AllFavList() on your service instead of all these chain of calls, it will be easier to mock.

EDIT2: How to mock you get property

dataService.SetupGet(d => d.FavLists).Returns(your_variable);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜