模拟IAuthSession.GetOAuthTokens

时间:2016-05-04 19:50:14

标签: c# unit-testing nunit servicestack moq

我有一个使用以下代码的服务堆栈服务

public MyResponse Get(MyRequest request){
    var authSession = GetSession();
    var tokens = authSession.GetOAuthTokens("somekey");

    var jwt = JwtPayload.Deserialize(tokens.AccessTokenSecret);
    var clientId = jwt.Claims.First(x => x.Type == "client_id").Value;

//...

我正在围绕这个服务函数创建一个单元测试,但似乎GetOAuthTokens是一个静态扩展方法,不能被Moq(或者那个例子中的任何Mocking框架)模拟。

这是我目前的测试功能

    [Test]
    public void Get_ShouldReturnListOfApplications()
    {

        using (new BasicAppHost(typeof(ApplicationService).Assembly).Init())
        {
            var req = new MockHttpRequest();
            req.Items[SessionFeature.RequestItemsSessionKey] =
                new AuthUserSession
                {
                    UserName = "Mocked",
                    RequestTokenSecret="test",
                    ProviderOAuthAccess = new System.Collections.Generic.List<IAuthTokens>() { MockTokens.Object }
                };

            using (var service = HostContext.ResolveService<ApplicationService>(req))
            {
                service.Client = MockServiceClient.Object;
                service.Settings = MockSettingsProvider.Object;

                var response = service.Get(TestApplicationRequest);

            }
        }
    }

无论如何都要嘲笑那个&#34; GetOAuthTokens&#34;功能

1 个答案:

答案 0 :(得分:2)

以下是GetOAuthTokens() extension method的实施:

import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;

var grav:int = 0;
var floor = 450;
var state:String = "stand";

stage.addEventListener(KeyboardEvent.KEY_DOWN , keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP , keyHandler);
stage.addEventListener(Event.ENTER_FRAME , gameLoop);

function keyHandler(e:KeyboardEvent):void {
    switch (e.keyCode) {
        case Keyboard.D:
            state = (e.type == "keyDown") ? "right" : "stand";
            break;
        case Keyboard.A:
            state = (e.type == "keyDown") ? "left" : "stand";
            break;
        case Keyboard.W:
            state = (e.type == "keyDown") ? "jumping" : "falling";
            break;
    }
}

function gameLoop(Levent:Event):void {  
    switch (state) {
        case "stand":
            kim.gotoAndStop("kim Stand");
            break;
        case "right":
            kim.x += 5;
            kim.gotoAndStop("kim Move Right");
            break;
        case "left":
            kim.x -= 5;
            kim.gotoAndStop("kim Move Left");
            break;
        case "jumping":
            kim.y -= 10;
            kim.gotoAndStop("kim Jump");
            break;
        case "falling":
            kim.gotoAndStop("kim Jump");
            break;
    }

    gravity();
}

function gravity ():void {
    kim.y += grav;
    if (kim.y + kim.height/2 < floor) {
        grav++;
    } else {
        grav = 0;
        kim.y = floor - kim.height/2;

        if (state == "falling") {
            state = "stand"
        }
    }
}

因此它应该返回public static IAuthTokens GetOAuthTokens(this IAuthSession session, string provider) { foreach (var tokens in session.ProviderOAuthAccess) { if (string.Compare(tokens.Provider, provider, StringComparison.InvariantCultureIgnoreCase) == 0) return tokens; } return null; }

session.ProviderOAuthAccess的所有令牌