gpt4 book ai didi

asp.net-mvc-3 - 需要帮助在需要 HttpContext.Current.User 的方法上编写单元测试

转载 作者:行者123 更新时间:2023-12-02 14:51:06 25 4
gpt4 key购买 nike

我正在尝试为我的 MVC3 项目编写一些单元测试(该项目的第一个测试),但我被难住了。基本上,我有一个 MemberQueries 类,我的 MemberController 使用它来处理所有逻辑。

我想开始在这个类上编写测试,并想从一个简单的例子开始。我在这个类中有一个名为 IsEditModeAvailable 的方法,它确定用户是否是“站点管理员”角色的成员,或者用户是否能够编辑自己的数据,但不能编辑其他人的数据。我通过将传入的 Id 值与 HttpContext User 属性进行比较来确定最后一个要求。

我遇到的问题是我不知道在创建 MemberQueries 对象时如何模拟或注入(inject)正确的参数到我的单元测试中。我正在使用NUnit、Moq 和Ninject,但我只是不确定如何编写代码。如果我的结构不正确,请告诉我,因为我对单元测试完全是菜鸟。

以下是我的 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();
}
}

最后,这是我尝试编写的单元测试的 stub :

[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 "jdoe@acme.com"

var memberServices = new Mock<MemberServices>();
// I then need to tell Moq that it's FindByEmail("jdoe@acme.com") 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 "jdoe@acme.com" who has an Id of 1, then IsEditModeAvailable(1) should return true
Assert.IsTrue(memberQueries.IsEditModeAvailable(1));
}

最佳答案

您似乎正在尝试测试 MemberQueries.IsEditModeAvailable 方法。这里有 2 个案例需要介绍。 站点管理员 情况以及当前登录用户的 id 与作为参数传递的用户的 id 匹配的情况。由于 MemberQueries 类纯粹依赖于接口(interface),因此您可以模拟所有内容:

[TestMethod]
public void EditMode_Must_Be_Available_For_Site_Administrators()
{
// arrange
var unitOfWork = new Mock<IUnitOfWork>();
var currentUser = new Mock<IPrincipal>();
currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(true);
var memberServices = new Mock<IMemberServices>();
var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);

// act
var actual = memberQueries.IsEditModeAvailable(1);

// assert
Assert.IsTrue(actual);
}

[TestMethod]
public void EditMode_Must_Be_Available_For_Logged_In_Users_If_His_Id_Matches()
{
// arrange
var unitOfWork = new Mock<IUnitOfWork>();
var currentUser = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
identity.Setup(x => x.Name).Returns("john.doe@gmail.com");
currentUser.Setup(x => x.Identity).Returns(identity.Object);
currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(false);
var memberServices = new Mock<IMemberServices>();
var member = new Member
{
Id = 1
};
memberServices.Setup(x => x.FindByEmail("john.doe@gmail.com")).Returns(member);
var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);

// act
var actual = memberQueries.IsEditModeAvailable(1);

// assert
Assert.IsTrue(actual);
}

实际上,您需要考虑第三种情况:您当前登录的用户不是站点管理员,并且其 ID 与作为参数传递的 ID 不匹配:

[TestMethod]
public void EditMode_Should_Not_Be_Available_For_Logged_In_Users_If_His_Id_Doesnt_Match()
{
// arrange
var unitOfWork = new Mock<IUnitOfWork>();
var currentUser = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
identity.Setup(x => x.Name).Returns("john.doe@gmail.com");
currentUser.Setup(x => x.Identity).Returns(identity.Object);
currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(false);
var memberServices = new Mock<IMemberServices>();
var member = new Member
{
Id = 2
};
memberServices.Setup(x => x.FindByEmail("john.doe@gmail.com")).Returns(member);
var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);

// act
var actual = memberQueries.IsEditModeAvailable(1);

// assert
Assert.IsFalse(actual);
}

关于asp.net-mvc-3 - 需要帮助在需要 HttpContext.Current.User 的方法上编写单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9368510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com