我想爲我的MVC3項目(這個項目的第一個測試)編寫一些單元測試,我越來越難倒了。基本上,我有一個MemberQueries類,我的MemberController用於處理所有的邏輯。需要幫助編寫單元測試需要HttpContext.Current.User的方法
我想開始在這個類上編寫測試,並想從一個簡單的例子開始。我在這個類中有一個名爲IsEditModeAvailable的方法,該方法確定用戶是否是「站點管理員」角色的成員,或者用戶是否能夠編輯自己的數據,但沒有人會刪除。我通過將傳入的Id值與HttpContext用戶屬性進行比較來確定最後一項要求。
我遇到的問題是我不知道如何在創建MemberQueries對象時將模擬或注入適當的參數到我的單元測試中。我正在使用,NUnit,Moq和Ninject,但我不知道如何編寫代碼。如果我只是沒有正確地構建它,請告訴我,因爲我是單元測試的完全noobot。
下面是從我MemberQueries類的代碼示例:
public class MemberQueries : IMemberQueries
{
private readonly IRepository<Member> _memberRepository;
private readonly IMemberServices _memberServices;
private readonly IPrincipal _currentUser;
public MemberQueries(IUnitOfWork unitOfWork, IMemberServices memberServices, IPrincipal currentUser)
{
_memberRepository = unitOfWork.RepositoryFor<Member>();
_memberServices = memberServices;
_currentUser = currentUser;
}
public bool IsEditModeAvailable(int memberIdToEdit)
{
if (_currentUser.IsInRole("Site Administrator")) return true;
if (MemberIsLoggedInUser(memberIdToEdit)) return true;
return false;
}
public bool MemberIsLoggedInUser(int memberIdToEdit)
{
var loggedInUser = _memberServices.FindByEmail(_currentUser.Identity.Name);
if (loggedInUser != null && loggedInUser.Id == memberIdToEdit) return true;
return false;
}
}
下面是從我MemberServices類樣品(這是我的域名項目,由MemberQueries引用):
public class MemberServices : IMemberServices
{
private readonly IRepository<Member> _memberRepository;
public MemberServices(IUnitOfWork unitOfWork)
{
_memberRepository = unitOfWork.RepositoryFor<Member>();
}
public Member Find(int id)
{
return _memberRepository.FindById(id);
}
public Member FindByEmail(string email)
{
return _memberRepository.Find(m => m.Email == email).SingleOrDefault();
}
}
最後,這裏是我試圖寫的單元測試的存根:
[Test]
public void LoggedInUserCanEditTheirOwnInformation()
{
var unitOfWork = new UnitOfWork();
var currentUser = new Mock<IPrincipal>();
// I need to somehow tell Moq that the logged in user has a HttpContext.User.Name of "[email protected]"
var memberServices = new Mock<MemberServices>();
// I then need to tell Moq that it's FindByEmail("[email protected]") method should return a member with a UserId of 1
var memberQueries = new MemberQueries(unitOfWork, memberServices.Object, currentUser.Object);
// If the logged in user is "[email protected]" who has an Id of 1, then IsEditModeAvailable(1) should return true
Assert.IsTrue(memberQueries.IsEditModeAvailable(1));
}
我沒有在代碼中看到任何地方引用了'HttpContext.Current'。 – 2012-02-20 21:17:49
Darin,我通過ninject注入HttpContext,如下所示:'kernel.Bind().ToMethod(ctx => HttpContext.Current.User).InRequestScope();' –
bigmac
2012-02-20 21:28:02
您試圖測試的代碼不取決於在上面。所以你可以很好地嘲笑一切。 – 2012-02-20 21:36:30