测试与 ASP 会员资格一起使用的自定义用户配置文件
我刚刚完成了在我的 MVC 项目中实现基本的 ASP 成员身份,并且我希望编写一些测试。我最初在嘲笑提供者时遇到了一些问题,但现在已经解决了。显然,由于提供者被模拟,其中没有数据,您也无法添加任何数据,但配置文件不能直接模拟,因为每次需要用户配置文件时都会创建它们。我不确定如何进行测试,因为(显然)当调用配置文件时它会返回 null。
例如:
使用 DI 我的存储库被注入:
public AccountMembershipService(MembershipProvider provider)
{
_provider = provider ?? Membership.Provider;
}
让我们以创建用户方法为例。
public MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer)
{
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
MembershipCreateStatus status;
_provider.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true, null, out status);
if (status == MembershipCreateStatus.Success)
{
UserProfile profile = UserProfile.GetProfile(userName);
profile.IPAddress = "0.0.0.0";
profile.Save();
}
return status;
}
.CreateUser 工作完美(就模拟而言),但是当涉及到 GetProfile 时,显然不存在配置文件,因此它只是错误。关于我如何能够对此进行测试有什么想法吗?
我已经盯着这个好几个小时了,任何帮助将非常感激!
I've just finished implementing a basic asp membership into my MVC project and I'm looking to write some tests. I had some initial problems with mocking the provider but now that is sorted. Obviously as the provider is mocked no data is in there nor can you add any data but the profile's cannot be directly mocked as they are created every time a user profile is required. I'm not sure how to approach testing these as (obviously) when a profile is called it returns null.
For example:
Using DI my repository is injected:
public AccountMembershipService(MembershipProvider provider)
{
_provider = provider ?? Membership.Provider;
}
And lets take for example, the create user method
public MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer)
{
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
MembershipCreateStatus status;
_provider.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true, null, out status);
if (status == MembershipCreateStatus.Success)
{
UserProfile profile = UserProfile.GetProfile(userName);
profile.IPAddress = "0.0.0.0";
profile.Save();
}
return status;
}
The .CreateUser works perfectly (As far as mocking is concerned) but when it comes to GetProfile obviously there is no profile in existence so it just errors. Any ideas on how I might be able to approach testing this?
I've been staring at this for a good couple of hours now, any help would be REALLY appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看使用 Pex Moles 替换 Base UserProfile 类的 Create 方法。
另一种选择是创建一个调用 GetProfile() 的服务对象,然后可以将其抽象出来,并在运行时将具体类注入到 AccountMembershipService 中,或者在单元测试期间传递给构造函数。
Have a look at using Pex Moles to replace Create method of the Base UserProfile class.
Another option could be to create a service object that makes the call to GetProfile(), this could then be abstracted and a concrete class injected to the AccountMembershipService at runtime, or passed to the constructor during unit tests.