使用 Moq 进行 FormsAuthentication.SetAuthCookie 模拟

2024-03-29

您好,我正在对 ASP.Net MVC2 项目进行一些单元测试。我正在使用起订量框架。在我的 LogOnController 中,

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl = "")
{
  FormsAuthenticationService FormsService = new FormsAuthenticationService();
  FormsService.SignIn(model.UserName, model.RememberMe);

 }

在 FormAuthenticationService 类中,

public class FormsAuthenticationService : IFormsAuthenticationService
    {
        public virtual void SignIn(string userName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot     be null or empty.", "userName");
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }

我的问题是如何避免执行

FormsService.SignIn(model.UserName, model.RememberMe);

这条线。或者有什么办法可以最小起订量

 FormsService.SignIn(model.UserName, model.RememberMe);

使用起订量框架without更改我的 ASP.Net MVC2 项目。


Inject IFormsAuthenticationService作为对你的依赖LogOnController像这样

private IFormsAuthenticationService formsAuthenticationService;
public LogOnController() : this(new FormsAuthenticationService())
{
}

public LogOnController(IFormsAuthenticationService formsAuthenticationService) : this(new FormsAuthenticationService())
{
    this.formsAuthenticationService = formsAuthenticationService;
}

第一个构造函数用于框架,以便正确的实例IFormsAuthenticationService在运行时使用。

现在在您的测试中创建一个实例LogonController通过传递模拟来使用其他构造函数,如下所示

var mockformsAuthenticationService = new Mock<IFormsAuthenticationService>();
//Setup your mock here

更改您的操作代码以使用私有字段formsAuthenticationService如下

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl = "")
{
    formsAuthenticationService.SignIn(model.UserName, model.RememberMe);
}

希望这可以帮助。我已经为您留下了模拟设置。如果您不确定如何设置,请告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Moq 进行 FormsAuthentication.SetAuthCookie 模拟 的相关文章

随机推荐

  • MongoDB 数据库,相当于 SELECT column1, column2 FROM tbl

    从我的 MongoDB 中我想要相当于 SELECT column1 column2 FROM tbl 通过这段代码 我得到了所有的 行 也得到了所有的 列 DBCollection collection database getColle
  • Winforms C# Outlook 风格日历

    我的任务是在 winforms C 应用程序中重新创建 MS Access 日历 我为用户创造的东西 他们讨厌 基本上 我正在将数据转储到 DataGridView 中 他们可以按月 日或员工进行搜索以获取日历事件 他们要求重新设计 使其看
  • Alamofire 具有用于快速应用程序的自定义参数编码

    我必须在我的 swift 应用程序中从 SOAP Web 服务调用一些方法 所以我认为我应该使用自定义参数编码 但是当我为此编码创建闭包时 它似乎永远不会被调用 难道我做错了什么 这是我的代码 let custom URLRequestCo
  • Spring 4,JPA,关闭控制台调试消息

    我有一个基本的 Spring 4 JPA 应用程序 我使用所有 Java 配置 根本没有 XML 我想关闭控制台调试消息 我看到了很多关于此的问题并尝试了解决方案 但我仍然看到了所有消息 控制台消息如下所示 14 58 29 301 mai
  • 从函数返回数据 (Swift)

    我正在尝试返回结果并能够从此函数访问结果数组 函数中的一切都正常工作 但是我无法返回任何内容或访问结果或从闭包外部在函数内部创建的任何变量 我想从闭包外部访问 result valueForKey id 我怎样才能做到这一点 class V
  • 错误(Xcode):意外的重复任务:目标“Runner”已将命令从“path/GoogleService-Info.plist”复制到“path/GoogleService-Info.plist”

    我尝试在正确使用 firebase 进行设置后在 iOS 上运行 flutter 项目 但收到以下与 GoogleService Info plist 相关的重复错误 这是完整的错误输出 Error output from Xcode bu
  • Angular 订阅将对象推送到数组

    我正在制作角度应用程序 并且我有一个空数组 例如 users any 然后我拨打服务电话ngOnInit将数据存储到users像数组一样 ngOnInit Getting the data from json this httpClient
  • 将 SSRS 从 2016 年降级至 2008 年

    我们有 2 个工作环境 一个用于 SSRS 2016 另一个用于 2008 我错误地在 VS2016 中打开了一份 2008 年的报告 现在我无法打开2008年的它 如何将 2016 年打开的 SSRS 报告降级回 2008 年 我设法做到
  • 在 C++ API 中将一个张量的一大块复制到另一个张量中

    我需要复制一行一个张量 在c API 转换为另一个张量的某些部分 其中开始和结束索引可用 在 C 中我们可以使用类似的东西 int myints 10 20 30 40 50 60 70 std vector
  • 自定义 Perforce RCS 关键字扩展的输出

    我想使用 RCS 关键字扩展来过滤文件 以便 Change 被翻译成1745而不是默认行为 Change 1745 我意识到这会阻止未来的扩展 但就目的而言这是可以接受的 也欢迎使用其他将更改列表编号插入文件的方法 这是我在 Perforc
  • 列表视图多重选择

    有没有办法强制列表视图控件将所有点击视为通过 Control 键完成的 我需要复制使用控制键的功能 选择项目集并取消设置其选择状态 以便允许用户轻松地同时选择多个项目 先感谢您 即使 MultiSelect 设置为 true 这也不是 Li
  • 逐字迭代字符串

    我想知道如何逐字迭代字符串 string this is a string for word in string print word 上面给出了一个输出 t h i s i s a s t r i n g 但我正在寻找以下输出 this
  • 反射值接口和指针接收器

    在golang的mongodb驱动中有以下代码 case reflect Struct if z ok v Interface Zeroer ok return z IsZero return false Zeroer 接口定义如下 typ
  • 比较 (int)double 和 (int)int 时出现异常

    嘿 我正在使用 pdCurses lib 和 stringStream 来计算并制作一个代表时钟的 5 个字符长的字符串 它显示为 00 00 0 00 00 00 或 0 000 但是 当运行我的函数时 我在这部分抛出一个异常 if in
  • 如何在Python中验证字典的结构(或模式)?

    我有一本包含配置信息的字典 my conf version 1 info conf one 2 5 conf two foo conf three False optional conf bar 我想检查字典是否遵循我需要的结构 我正在寻找
  • 有没有办法给某人打电话并在android中播放音频文件?

    我想创建一个紧急呼叫应用程序 如果触发 它会呼叫给定号码并播放音频文件 提供呼叫者无法提供的信息 为此 我需要拨打电话 但确保我可以用播放的音频文件替换扬声器中的任何声音 我可以在安卓中做到这一点吗 有什么办法呢 目前您无法使用 G1 执行
  • 自定义“AuthenticationStateProvider”身份验证失败

    我创建了一个自定义ApiAuthenticationStateProvider返回后AuthenticationState仍在说明 info Microsoft AspNetCore Authorization DefaultAuthori
  • Genymotion 中运行的虚拟设备会定期在 ADB 中离线

    我有一个在 Genymotion 2 4 中运行的 Android 设备 如果重要的话 带有 Lollipop 图像 并通过 ADB 版本 1 0 32 从本地网络中使用 Eclipse 的另一台开发人员 PC 连接到它 没有连接问题 一切
  • 如何使用 swift 3 通过 firebase 推送通知发送图像

    任何人都可以帮我发送这样的通知 我正在使用 Firebase 通知 当我发送通知时 我尝试将图像 URL 放入高级选项和键 1 的值中 图像 URL 显示在调试器中 但当我的设备中出现通知时 没有显示图像 这是我的代码 import UIK
  • 使用 Moq 进行 FormsAuthentication.SetAuthCookie 模拟

    您好 我正在对 ASP Net MVC2 项目进行一些单元测试 我正在使用起订量框架 在我的 LogOnController 中 HttpPost public ActionResult LogOn LogOnModel model str