我正在尝试使用InMemory EF7数据库进行我的xunit存储库测试.
但我的问题是,当我尝试Dispose创建的上下文时,内存db仍然存在.这意味着一个测试涉及其他.
我已阅读这篇文章单元测试实体框架7与内存中的数据存储和我试图安装在我的TestClass的构造背景.但这种方法不起作用.当我单独运行测试时一切正常,但我的第一个测试方法是在DB中添加一些东西,第二个测试方法从之前的测试方法开始使用脏DB.我尝试添加IDispose到测试类,但方法DatabaseContext和DB在内存中持久存在.我错了什么我错了什么?
我的代码看起来像:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Fabric.Tests.Repositories
{
/// <summary>
/// Test for TaskRepository
/// </summary>
public class TaskRepositoryTests:IDisposable
{
private readonly DatabaseContext contextMemory;
/// <summary>
/// Constructor
/// </summary>
public TaskRepositoryTests()
{
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseInMemoryDatabase();
contextMemory = new DatabaseContext(optionsBuilder.Options);
}
/// <summary>
/// Dispose DB
/// </summary>
public void Dispose()
{
//this has no effect
if (contextMemory != null)
{ …Run Code Online (Sandbox Code Playgroud) 在以前的ASP.NET MVC版本中,向模型添加自定义验证的方法是实现IValidatableObject并实现自己的Validate()方法.举个例子:
public class BestModelEver : IValidatableObject {
public DateTime? Birthday { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
if (Birthday.HasValue) {
yield return new ValidationResult("Error message goes here");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这仍然是向ASP.NET Core中的模型添加自定义验证的推荐方法吗?使用依赖IValidatableObject于System.ComponentModel.DataAnnotations依赖.
我有一个基本路由的简单API.它是使用默认的Visual Studio 2015 ASP.NET Core API模板设置的.
我有这个控制器和动作:
[Route("api/[controller]")]
public class DocumentController : Controller
{
[HttpGet("info/{Id}")]
public async Task<Data> Get(string Id)
{
//Logic
}
}
Run Code Online (Sandbox Code Playgroud)
所以要达到这个方法,我必须打电话GET /api/document/info/some-id-here.
是否可以使用.NET Core,在该方法中,以字符串形式检索完整路径?
所以我可以这样做:
var myRoute = retrieveRoute();
// myRoute = "/api/document/info/some-id-here"
Run Code Online (Sandbox Code Playgroud) UseJwtBearerAuthenticationASP.NET Core中的中间件可以轻松验证Authorization标头中传入的JSON Web令牌.
如何验证通过cookie而不是标头传递的JWT?有点像UseCookieAuthentication,但对于只包含JWT的cookie.
我正在尝试构建一个多目标.NET 4.5.1和.NET Standard 1.3的类库.根据文档,我应该能够这样做:
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试构建时,我得到了这些奇怪的错误:
无法从TargetFramework ='net451'推断出TargetFrameworkIdentifier和/或TargetFrameworkVersion.必须明确指定它们.
找不到MSB3645 .NET Framework v3.5 Service Pack 1.要以".NETFramework,Version = v1.3"为目标,必须安装.NET Framework v3.5 Service Pack 1或更高版本.
MSB3644找不到 框架".NETFramework,Version = v1.3"的引用程序集.要解决此问题,请为此框架版本安装SDK或Targeting Pack,或者将应用程序重新定位到已安装SDK或Targeting Pack的框架版本.请注意,程序集将从全局程序集缓存(GAC)中解析,并将用于代替引用程序集.因此,您的程序集可能无法正确定位到您想要的框架.
如果我手动指定目标框架标识符,它构建正常:
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net451'">
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2017社区.我在这里做错了吗?
我需要在watchOS 2中获取WatchKit App的用户当前位置.我该怎么办?
project.json会不会消失,而web.config会在ASP.NET Core 1.0中重新出现?
在路线图文档中,在2016年第四季度或第一季度的更新中,他们提到"用.csproj/MSBuild替换.xproj/project.json".
这标志着project.json的结束吗?
我最近一直在研究.NET Core Web API.我刚刚通过https://stormpath.com/blog/token-authentication-asp-net-core上的指南尝试使用JWT进行身份验证.
一切顺利,直到我不得不GetIdentity用数据库查询替换方法中的硬编码用户名和密码,并意识到我不知道如何从这个文件中访问数据库!
我所指的方法显示在第70行的链接中 .https://github.com/nbarbettini/SimpleTokenProvider/blob/master/test/SimpleTokenProvider.Test/Startup.Auth.cs
我的问题如下.
我正在使用xUnit 2.0 集合夹具来共享许多不同测试类之间的公共数据库设置/拆卸.fixture还提供了一些辅助属性,因此我将它注入每个测试类.
我在文档中重新创建了示例,但是当我运行测试时,它立即失败:
以下构造函数参数没有匹配的fixture数据:IntegrationTestFixture fixture
无论我是使用xUnit Facts还是使用Theories,或者我正在使用哪个测试运行器,这似乎都会发生.
夹具:
public class IntegrationTestFixture : IDisposable
{
public IntegrationTestFixture()
{
// (setup code)
this.GeneratedTestName = [randomly generated];
}
public void Dispose()
{
// (teardown code)
}
public string GeneratedTestName { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
集合定义:
[CollectionDefinition("Live tests")]
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture>
{
// Intentionally left blank.
// This class only serves as an anchor for CollectionDefinition.
}
Run Code Online (Sandbox Code Playgroud)
测试:
[CollectionDefinition("Live tests")]
public class SomeTests
{
private readonly IntegrationTestFixture fixture;
public …Run Code Online (Sandbox Code Playgroud) 我设置了各种C#代码样式规则来生成错误,虽然违规在IDE中显示为错误(错误列表和文本编辑器中),但实际构建仍然成功.
谁能证实这一点?我在社区(家庭)和企业(工作)版本的VisualStudio/15.0.0 + 26228.9上进行了测试.由于代码样式违规,我无法获得任何构建.
我甚至尝试使用.editorconfig,并且构建仍然经历...
.net ×6
.net-core ×5
asp.net-core ×5
c# ×5
apple-watch ×1
cookies ×1
csproj ×1
editorconfig ×1
jwt ×1
location ×1
msbuild ×1
objective-c ×1
project.json ×1
watchkit ×1
watchos-2 ×1
web-config ×1
xunit.net ×1
xunit2 ×1