我開始做一些單元測試的實驗,以便我們可以將它們包含在我們的域層中。但是我不知道我是否遵循正確的道路,因此我要解釋我目前正在做的事情,看看我是否在正確的軌道上。基本上這個體系結構就像下面的域層包含域模型和域服務(例如User類和UserService類)。然後域層與實現通用存儲庫模式的DAL一起與工作單元進行通信。在它的構造函數中的每個域服務類接受一個IUnitOfWork界面,如下所示:FakeIt易於測試域服務+ UnitOfWork
public class UserService: IUserService
{
private readonly IUnitOfWork _unitOfWork;
public UserService(IUnitOfWork unitOfwork)
{
this._unitOfWork = unitOfwork;
}
}
爲了創造單元測試,我決定去與FakeItEasy框架。所以在一個UserServiceTest類我做了以下事情: -
private IUserService _userService;
private const int userID = 2013;
[TestInitialize]
public void Initialize()
{
_userService = A.Fake<IUserService>();
A.CallTo(() => _userService.GetUserById(userID)).Returns(new User
{
UserID = userID,
RegistrationDate = DateTime.Now,
});
}
[TestMethod]
public void GetUserByID()
{
var user = _userService.GetUserById(userID);
Assert.IsInstanceOfType(user, typeof(Domain.User));
Assert.AreEqual(userID, user.userID);
}
當我運行測試時,它們通過。這是實施單元測試的正確方法嗎?在我嘗試一種不同的方法之前,FakeItEasy因ProxyGenerator異常而失敗。我在做什麼,這是: -
[TestInitialize]
public void Initialize()
{
_unitOfWork = A.Fake<IUnitOfWork>();
A.CallTo(() => _unitOfWork.UserRepository.FindById(userID)).Returns(new UserDto
{
UserID = userID,
RegistrationDate = DateTime.Now,
});
AutoMapper.Mapper.CreateMap<UserDto, User();
}
[TestMethod]
public void GetUserByID()
{
var userService = new UserService(_unitOfWork);
var user = userService.GetUserById(userID);
Assert.IsInstanceOfType(user, typeof(Domain.User));
Assert.AreEqual(userID, user.userID);
}
,這是拋出異常如下: -
Result Message:
Initialization method Initialize threw exception. System.ArgumentNullException: System.ArgumentNullException: Value cannot be null.
Parameter name: callTarget.
Result StackTrace:
at FakeItEasy.Creation.ProxyGeneratorSelector.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String& failReason)
at FakeItEasy.Configuration.DefaultInterceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget)
at FakeItEasy.Configuration.FakeConfigurationManager.AssertThatMemberCanBeIntercepted(LambdaExpression callSpecification)
at FakeItEasy.Configuration.FakeConfigurationManager.CallTo[T](Expression`1 callSpecification)
at FakeItEasy.A.CallTo[T](Expression`1 callSpecification)
任何反饋將不勝感激。謝謝!
你的'UserService'看起來像存儲庫。我會將它們視爲DAL的一部分。當Domain Model知道抽象「IUnitOfWork」,但你的域名服務與[Big Blue Book](http:// dddcommunity)中的描述不同時,org/book/evans_2003 /)([域驅動設計中的服務](http://gorodinski.com/blog/2012/04/14/services-in-domain-driven-design-ddd/),[Services in Domain-Driven Design](http://lostechies.com/jimmybogard/2008/08/21/services-in-domain-driven-design/))。 –