我有一个类来处理密码更改和交换邮箱的到期检查.我在UserPrincipal上检查LastPasswordSet.
那TDD怎么样?
我想通过编写一些测试来检查我的类是否正确处理了密码检查.但我无法理解我如何模拟UserPrincipal.FindByIdentity(principalContext,[some username]).
如果密码在过去90天内被更改,我即将编写一个返回true/false的方法.所以我想模拟UserPrincipal,这样我就可以在我的测试中设置LastPasswordSet返回值,只是为了检查我要编写的"密码需求更改通知"的逻辑.
cst*_*ele 11
我意识到这是一个老帖子,但最近我遇到了与原始海报相同的问题,在看完答案之后,我想我必须做同样的事情.我不希望别人读这篇文章,像我一样浪费宝贵的时间,所以我决定真正回答这篇文章.
在意识到没有简单的方法来包装UserPrincipal功能,也不依赖于像建议的集成或端到端测试之后,我记得有一种模拟静态类的方法.话虽如此,下面是使用Telerik JustMock的确切方法.
private void MockUserPrincipal()
{
//Mock principal context
var context = Mock.Create<PrincipalContext>((action) => action.MockConstructor());
//Mock user principal
var user = Mock.Create(() => new UserPrincipal(context));
//Mock the properties you need
Mock.Arrange(() => user.Enabled).Returns(true);
Mock.Arrange(() => user.UserPrincipalName).Returns("TestUser");
Mock.Arrange(() => user.LastPasswordSet).Returns(DateTime.Now);
//Mock any functions you need
Mock.Arrange(() => user.IsAccountLockedOut()).Returns(false);
//Setup static UserPrincipal class
Mock.SetupStatic<UserPrincipal>();
//Mock the static function you need
Mock.Arrange(() => UserPrincipal.FindByIdentity(Arg.IsAny<PrincipalContext>(), Arg.AnyString)).Returns(user);
//Now calling UserPrincipal.FindByIdentity with any context and identity will return the mocked UserPrincipal
}
Run Code Online (Sandbox Code Playgroud)
我会用简洁的短语来回答这个问题
找不到权威的博文来支持这一点。示例链接。尽管这似乎归功于乔·沃尔恩斯。
如果我记得的话,UserPrincipal 是一个与身份验证相关的.Net 框架类。超出您控制(可以更改)的模拟类型可能会导致脆弱的测试。
相反,从 UserPrincipal 中发现您的设计想要什么
| 归档时间: |
|
| 查看次数: |
5553 次 |
| 最近记录: |