Unit testing 通过单元测试中的会话
我正在为我的服务编写单元测试。我使用Azure Active Directory进行身份验证。现在,当使用Unit testing 通过单元测试中的会话,unit-testing,azure,servicestack,Unit Testing,Azure,servicestack,我正在为我的服务编写单元测试。我使用Azure Active Directory进行身份验证。现在,当使用MockHttpRequest传递会话时,我遇到异常,因为无法将类型为“ServiceStack.AuthUserSession”的对象强制转换为类型为“AadAuthSession”。这是我在Testbase类中的代码 IAuthSession session = new AadAuthSession(); session.UserName = "Author";
MockHttpRequest
传递会话时,我遇到异常,因为无法将类型为“ServiceStack.AuthUserSession”的对象强制转换为类型为“AadAuthSession”。
这是我在Testbase类中的代码
IAuthSession session = new AadAuthSession();
session.UserName = "Author";
appHost.Plugins.Add(new AuthFeature(() => session,
new IAuthProvider[] {
new AadAuthProvider(appHost.AppSettings),
}
));
我是否遗漏了什么……如何正确地施放
public TestBase()
{
appHost = new TestAppHost().Init();
appHost.TestMode = true;
appHost.AppSettings = new AppSettings();
appHost.Container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));
appHost.Container.Register<ICacheClient>(new MemoryCacheClient());
IAuthSession session = new AadAuthSession();
appHost.Container.Register<IAuthSession>(c => session);
session.UserName = "Author";
appHost.Plugins.Add(new AuthFeature(() => session,
new IAuthProvider[] {
new AadAuthProvider(appHost.AppSettings),
}
));
}
publictestbase()
{
appHost=newtestapphost().Init();
appHost.TestMode=true;
appHost.AppSettings=新的AppSettings();
appHost.Container.Register(
新的OrmLiteConnectionFactory(“:memory:”,sqlitedialent.Provider));
appHost.Container.Register(newmemorycacheclient());
IAuthSession session=new AadAuthSession();
appHost.Container.Register(c=>session);
session.UserName=“Author”;
appHost.Plugins.Add(新的AuthFeature(()=>会话,
新IAuthProvider[]{
新的AadAuthProvider(appHost.AppSettings),
}
));
}
如果您使用以下方法将AppHost设置为TestMode
,则可以使用IOC模拟会话:
SetConfig(new HostConfig { TestMode = true });
这将允许您注册在请求会话时返回的会话,即:
IAuthSession session = new AadAuthSession();
container.Register<IAuthSession>(c => session);
我正在使用模拟会话到IOC的第一个选项,并将TestMode设置为true,但仍然得到相同的错误..是否缺少或需要任何类型的强制转换..我已更新了代码..请查看帖子..@Lara您是否使用最新的v4.0.50版本的ServiceStack?如果是这样的话,StackTrace是什么?MockHttpRequest可能要求您从AuthUserSession继承。我已经为
MockHttpRequest()
添加了所需的代码,请查看我更新的帖子,并告诉我需要添加什么…@Lara您不必回答我的任何问题???@Lara请不要在评论中粘贴代码或转储输出,请用完整的StackTrace更新您的问题。还要添加您正在使用的ServiceStack版本-请在以后的所有问题中执行此操作。但是我希望像异常一样,您需要从AuthUserSession
继承。
req.Items[SessionFeature.RequestItemsSessionKey] = session;