使用最小起订量的 ASP.Net MVC TDD

发布于 2024-10-10 02:35:13 字数 1604 浏览 3 评论 0原文

我正在尝试使用 NUnit 和 Moq 来学习 TDD/BDD。

我一直遵循的设计传递了一个DataService类到我的控制器以提供对存储库的访问。

我想模拟 DataService 类以允许测试控制器。

有很多模拟传递给控制器​​的存储库的示例,但我无法弄清楚如何在这个

场景中模拟 DataService 类。

有人可以解释一下如何实现这个吗?

这是相关代码的示例:

[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);
        }

    }
}

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);
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-10-17 02:35:13

创建一个像这样的接口:

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

让您的 DataService 实现这个接口,并且您的控制器应该依赖于这个接口。问题已解决:)

编辑:这行代码:

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

违反了德米特定律< /a> 并且对您的服务进行单元测试将是一件痛苦的事情。在您的服务上创建一个类似 AllFavList() 的方法,而不是所有这些调用链,这样会更容易模拟。

编辑2:如何模拟你获得财产

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文