且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

写单元测试使用User.Identity.Name中的ASP.NET Web API方法

更新时间:2023-02-16 23:11:25

下面一个是这样做的只有一个办法:

The below one is only one way of doing this:

public class FooController : ApiController {

    public string Get() {

        return User.Identity.Name;
    }
}

public class FooTest {

    [Fact]
    public void Foo() {

        var identity = new GenericIdentity("tugberk");
        Thread.CurrentPrincipal = new GenericPrincipal(identity, null);
        var controller = new FooController();

        Assert.Equal(controller.Get(), identity.Name);
    }
}