我有一些遗留代码,我想要进行测试.这是必需品的复制品:
public class LegacyUnit
{
private readonly ICollaborator collaborator;
public LegacyUnit(ICollaborator collaborator)
{
this.collaborator = collaborator;
}
public object GetStuff(HttpContextBase context, string input)
{
try
{
if (input == "")
{
context.Response.End();
}
collaborator.DoOtherStuff();
return "Done!";
}
catch (ThreadAbortException)
{ }
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这个遗留单元存在一些问题,但是现在我只是试图让它受到考验.具体来说,我想测试,如果提出a ,collaborator.DoOtherStuff则不会调用.Response.End()ThreadAbort
问题:你如何提出这样的例外?
我已经阅读了这个问题及其答案ThreadAbortException,并了解它的特殊之处.但是,我从这些帖子中看不到如何在单元测试中处理这个问题.
这是我的尝试:
[Test]
public void DoesNotCallCollaboratorOnThreadAbort()
{
var testResponseMock = new Mock<HttpResponseBase>();
var testContextMock = new Mock<HttpContextBase>();
var collaboratorMock = new Mock<ICollaborator>();
testContextMock.Setup(x …Run Code Online (Sandbox Code Playgroud) 在这种情况下:
public class FooContext : DbContext
{
public FooContext(DbContextOptions<FooContext> opts) : base(opts)
{ }
public DbSet<Bar> Bars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我可以通过Bar两种方式达到目标:
fooContext.Bars.Add(new Bar()); // Approach 1
Run Code Online (Sandbox Code Playgroud)
要么
fooContext.Set<Bar>().Add(new Bar()); // Approach 2
Run Code Online (Sandbox Code Playgroud)
两种方法有什么区别?
我试图通过以下方式回答自己的问题:
Set<T>()还会创建一个DbSet<T>)DbSet<T>专门针对文档网址,但似乎没有相关结果DbSet<T>文档这只是表明您可以通过这两种方法的得到一组(如果不存在或不是差)Set<T>()文档但是我找不到关于将两者中的哪一个用于哪个目的的任何很好的解释。有什么不同?或更重要的是:我应该在哪里以及如何在文档中找到它?
我将在这些文档之后创建ASP.NET Core集成测试(基于xUnit)。我想用它自己的启动测试Web服务器appsettings.json。我的缩写文件夹结构是:
\SampleAspNetWithEfCore
\SampleAspNetWithEfCore\SampleAspNetWithEfCore.csproj
\SampleAspNetWithEfCore\Startup.cs
\SampleAspNetWithEfCore\appsettings.json
\SampleAspNetWithEfCore\Controllers\*
\SampleAspNetWithEfCore.Tests\SampleAspNetWithEfCore.Tests.csproj
\SampleAspNetWithEfCore.Tests\IntegrationTests.cs
\SampleAspNetWithEfCore.Tests\appsettings.json
Run Code Online (Sandbox Code Playgroud)
然后我有这些实用程序:
public static class ServicesExtensions
{
public static T AddOptions<T>(this IServiceCollection services, IConfigurationSection section)
where T : class, new()
{
services.Configure<T>(section);
services.AddSingleton(provider => provider.GetRequiredService<IOptions<T>>().Value);
return section.Get<T>();
}
}
Run Code Online (Sandbox Code Playgroud)
在内部,Startup.cs ConfigureServices(...)我这样做:
services.AddOptions<SystemOptions>(Configuration.GetSection("System"));
Run Code Online (Sandbox Code Playgroud)
appsettings.json像这样引用该部分:
"System": {
"PingMessageSuffix": " suffix-from-actual-project"
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好:这是通过强类型化方式获得的。我的控制器获得了一个SystemOptions反映json结构的实例,并且控制器正确使用了后缀。
问题在于构建集成测试WebHost。我想Startup使用自己的appsettings.json设置按原样运行真实项目,但是作为额外的一层设置,我希望添加appsettings.json 测试csproj中的设置,并覆盖所有适用的设置。这是我来自测试项目的设置:
"System": {
"PingMessageSuffix": " suffix-from-test-appsettings"
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
public class CustomWebApplicationFactory : WebApplicationFactory<Startup> …Run Code Online (Sandbox Code Playgroud) 我使用 Swashbuckle 和Redoc来记录我的 ASP.NET Core 2.2 API。实时 ReDoc 演示在顶部有一组部分(例如“简介”),其中包含一些自定义 html。我想在 API 中生成类似的部分,但不知道如何执行。
基本上我有:
services.AddSwaggerGen(c => {
c.SwaggerDoc(...);
c.IncludeXmlComments(...);
c.AddSecurityDefinition("OAuth2", ...);
});
Run Code Online (Sandbox Code Playgroud)
然后:
app.UseReDoc(c => {
c.SpecUrl = "/swagger/v1/swagger.json";
c.RoutePrefix = "";
});
Run Code Online (Sandbox Code Playgroud)
我已经浏览了 intellisense 选项以及 Swashbuckle readme和wiki,但找不到生成此类部分的方法。
将 HTML 部分添加到基于 Swashbuckle.AspNetCore.ReDoc 的文档的开头的方法是什么?
我试图使用failSpecWithNoExpectations从茉莉花配置文档通过设置它karma.conf.js:
module.exports = function (config) {
config.set({
failSpecWithNoExpectations: true,
// etc. all defaults from `ng new`
});
};
Run Code Online (Sandbox Code Playgroud)
但它没有被拿起,即使在重新启动ng test --watch. 我一直看到:
SPEC 没有期望应该做 blahblah
但是作为成功,没有报告失败。
我尝试了几件事:
ng new)代码库中搜索“茉莉花”,但没有明显的配置方法我没有想法......在 Angular 8+ CLI 应用程序中配置 Jasmine 选项的惯用位置是什么?
我正在尝试在我的 Angular 10 应用程序中创建一个APP_INITIALIZER,其中工厂具有多个依赖项。一个依赖项是 an InjectionToken<string>,但我无法像使用构造函数那样注入它。我得到:
错误...
装饰器在这里无效。
以下是重现此行为的方法(另请参阅此 StackBlitz 示例):
export const FOOBAR = new InjectionToken<string>('FOOBAR');
@Injectable({providedIn: 'root'})
export class FooService { dummy = 'value'; }
function initializerFactory(
fooService: FooService,
@Inject(FOOBAR) fooBar: string,
) {
return () => {
fooService.dummy = `Changed ${fooService.dummy}, added ${fooBar}`;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在app.module:
providers: [
{ provide: FOOBAR, useValue: 'token value' },
{ provide: APP_INITIALIZER, useFactory: initializerFactory, multi: true, deps: [FooService, FOOBAR] },
],
Run Code Online (Sandbox Code Playgroud)
如何使用 Angular 将 …
我有一张表有超过3.8亿条记录.我有一个存储过程:
整个过程大约需要30分钟才能执行.这DELETE需要28分钟.
删除是一个简单的陈述,有以下几点:
Delete a where condition_1 AND condition_2 AND condition_3
Run Code Online (Sandbox Code Playgroud)
有谁能够帮我?
你如何IF在jQuery模板中使用语句?
这是我的模板:
<script type="text/html" id="attachmentListTemplate">
{{if $item.current_cmt_id == id }}
<li>
<a href="${link}" class="fancyIFrame clearfix">${title}</a>
</li>
{{/if}}
</script>
Run Code Online (Sandbox Code Playgroud)
其中id基本上${id}是由数据绑定传递的(通过KnockoutJS).没有IF语句的输出很好,如下:${$item.current_cmt_id}
这是数据绑定(由KnockoutJS提供):
<ul data-bind='template: { name: "attachmentListTemplate", foreach: attachmentsModel.convAttachments, templateOptions: {current_cmt_id: <%=comment.id%>} }'> </ul>
Run Code Online (Sandbox Code Playgroud)
有关为什么if sttatement无效的任何建议?我正确地比较了这两个条件吗?
我正在尝试使用knockoutjs 1.2.l并使用以下代码
$(function() {
var viewModel = {
categories: ko.observableArray([
{"Selected": false, "Text": "Rooms", "Value": "1"},
{"Selected": false, "Text": "Automobile", "Value": "2"},
{"Selected": false, "Text": "Buy & Sell", "Value": "3"},
{"Selected": false, "Text": "Tutions", "Value": "4"},
{"Selected": false, "Text": "Immigration", "Value": "5"}
]),
initialData: {
"Description": null,
"SubCategoryId": 0,
"Title": null,
"UserId": 0,
"AdTypeId": 0,
"AddressId": null,
"SubCategory": null,
"User": null,
"AdType": null,
"Address": null,
"Id": 0,
"CreatedOn": "\/Date(1307627158991)\/",
"CreatedBy": 0,
"LastUpdatedOn": "\/Date(1307627158991)\/",
"LastUpdatedBy": 0
},
chosenCategory: ko.observable()
};
ko.applyBindings(viewModel); …Run Code Online (Sandbox Code Playgroud) 我试图让knockout.js在ajax调用后更新我的视图,但没有运气.我一直收到下面的错误.它出错了app.interactive.updateBindings.parseBindingAttribute viewModel参数未定义.
错误:
无法解析绑定属性.消息:
ReferenceError:interactive未定义;
属性值:template: { name: 'answerTmpl', foreach: interactive.answers }
动态HTML:
<div id="questions">
<div id="answers" data-bind="template: { name: 'answerTmpl', foreach: interactive.answers }">
</div>
</div>
<script type="text/javascript">
app.viewModel.interactive.answers(@Html.Raw(@Model.Answers.ToJson()));
app.interactive.updateBindings(document.getElementById('answers'));
</script>
<script id="answerTmpl" type="text/html">
<div>
<span></span> <input type="checkbox" name="Answer" />
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
我的.js文件:
(function (app, $, undefined) {
app.viewModel = {};
app.interactive = {};
app.interactive.callback = function () { };
app.viewModel.interactive = {
content: ko.observable('test'),
answers: ko.observableArray()
};
app.interactive.init = function () {
ko.applyBindings(app.viewModel);
}; …Run Code Online (Sandbox Code Playgroud) c# ×4
knockout.js ×3
angular ×2
asp.net-core ×2
.net ×1
ajax ×1
html ×1
jasmine ×1
javascript ×1
jquery ×1
karma-runner ×1
knockout-1.2 ×1
moq ×1
performance ×1
redoc ×1
sql-server ×1
swagger ×1
swashbuckle ×1
typescript ×1
unit-testing ×1
xunit ×1