Controller 类的单元测试

发布于 2025-01-14 14:49:44 字数 941 浏览 2 评论 0原文

您好,我正在尝试为函数编写一个简单的单元测试。

该函数是从Controller类中获取一些东西并从中获取特定的项目。

这是我想要进行单元测试的代码(在 Helper 类中):

public static string GetMemberCode(Controller controller)
{
    var claim = controller.User.Claims.FirstOrDefault(c => c.Type == "membercode");
    return (claim != null) ? claim.Value : string.Empty;
}

调用此函数的方式是使用(超过 1 个控制器类将调用此函数)

string membercode = Helper.GetMemberCode(this)

这就是我现在在单元测试中所拥有的内容

[Theory]
[InlineData("asdfqwer")]
public void GetMemberCode_IfControllerIsValid(string membercode)
{
    //Arrange
    var controller = new Controller(); //ERROR:CS0144 Cannot create an instance of the abstract type or interface 'Controller'
    //TODO: How to assign value inside?
    //Act
    var result = Helper.GetMemberCode(controller);
    //Assert
    result.Should().Be(membercode);
}

所以我的问题是如何在 Controller 类中赋值?

提前致谢。

Hi I am trying to write a simple unit test for a function.

That function is to get something from the Controller class and get a specific item from it.

This is the code that I wanted to do unit test from (In Helper class):

public static string GetMemberCode(Controller controller)
{
    var claim = controller.User.Claims.FirstOrDefault(c => c.Type == "membercode");
    return (claim != null) ? claim.Value : string.Empty;
}

The way this function is called is by using (More than 1 Controller class will be calling this)

string membercode = Helper.GetMemberCode(this)

This is what I have in my unit testing for now

[Theory]
[InlineData("asdfqwer")]
public void GetMemberCode_IfControllerIsValid(string membercode)
{
    //Arrange
    var controller = new Controller(); //ERROR:CS0144 Cannot create an instance of the abstract type or interface 'Controller'
    //TODO: How to assign value inside?
    //Act
    var result = Helper.GetMemberCode(controller);
    //Assert
    result.Should().Be(membercode);
}

So my question is How to assign value inside the Controller class?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文