使用最小起订量的 ASP.Net MVC TDD
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个像这样的接口:
让您的 DataService 实现这个接口,并且您的控制器应该依赖于这个接口。问题已解决:)
编辑:这行代码:
违反了德米特定律< /a> 并且对您的服务进行单元测试将是一件痛苦的事情。在您的服务上创建一个类似
AllFavList()
的方法,而不是所有这些调用链,这样会更容易模拟。编辑2:如何模拟你获得财产
Create an interface like this:
Make your DataService implement this interface and your controller should depend on this interface. Problem solved :)
EDIT: This line of code:
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