Tag: xunit

错误:“此提供程序无法使用空间类型和function”

我收到了错误: 空间类型和函数不适用于此提供程序,因为无法找到程序集“Microsoft.SqlServer.Types”版本10或更高版本。 我只在我的一台开发机器上出现此错误 – 另一方面,它正常工作。 两台机器都安装了SQL Server 2014,这应该足够了,但显然不是。 除此之外,我在解决方案中安装了nuget包“Microsoft.SqlServer.Types”(我已经在几个从属项目中尝试过了),并添加了相应的行来加载有问题的DLL: Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory); 调试显示有问题的行执行,并踩到它,似乎有问题的DLL加载成功。 用于运行项目(unit testing组件)的同一台机器没有错误。 错误开始出现的时间与我将测试项目从MSTest切换到xUnit的时间差不多,所以它可能与此有关。 有什么建议可以排除故障吗

只有1个Main方法的XUnit测试项目:“程序定义了多个入口点。”

我将vNext格式的.NET xUnit测试项目(带有project.json)转换为Visual Studio 2017 RC中的新.csproj格式,并开始收到以下错误。 这个错误的大多数在线答案都说“你有两个主要方法;摆脱一个。” 这似乎是一个明显的解决方案,但这个项目只有一个Main方法。 错误: CS0017程序定义了多个入口点。 使用/ main编译以指定包含入口点的类型。 Project.Name C:\ path \ to \ Program.cs Program.cs中: using XunitProgram = Xunit.Runner.DotNet.Program; namespace My.Namespace.Tests { public static class Program { public static void Main(string[] args) { XunitProgram.Main(args); } } } 老project.json: { “version”: “1.0.0-*”, “testRunner”: “xunit”, “buildOptions”: { “emitEntryPoint”: true, “preserveCompilationContext”: true, “debugType”: “full” […]

如何实现XUnit描述性断言消息?

上下文 在XUnit github中我发现了这个: 添加Assert.Equal(预期,实际,消息)重载#350 (所以开发人员要求不存在的重载请参见下文) 引用答案: 我们相信自我记录的代码; 包括你的断言。 (所以XUnit团队拒绝它) 好,我知道了。 我也相信自我记录代码。 我还是找不到这个用例: 样品 // Arrange // Create some external soap service client and its wrapper classes // Act // client.SomeMethod(); // Assert // Sorry, soap service’s interface, behaviour and design is *given* // So I have to check if there is no Error, and // […]

如何将服务配置传递给Xunit项目测试控制器?

我目前有以下设置。 启动类: Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSection(“AzureStorageConfig”)); services.AddTransient(); } //other config settings … } 类: AzureStorageConfig //store the azure account details etc… public class AzureStorageConfig { public string AzureURL { get; set; } public string […]

AutoFixture将PropertyData与多个条目和AutoData混合(使用AutoMoqCustomization)

我看过这两个类似的SO问题: AutoFixture:PropertyData和异构参数 AutoFixture CompositeDataAttribute不适用于PropertyDataAttribute 他们很棒,让我差不多到了那里。 但是这两个示例在发出的IEnumerable PropertyData中只使用了一个条目(即: yield return new object[] { 2, 4 }; – 请参阅: https : //stackoverflow.com/a/16843837/201308 )这有效,但它每当我想对多个对象[]测试数据进行测试时就会爆炸。 我有一整套想要发送的测试数据。 我在想这里的答案( https://stackoverflow.com/a/19309577/201308 )与我的需求类似,但我无法弄清楚。 我基本上需要AutoFixture为PropertyData的每次迭代创建一个sut实例。 一些参考: public static IEnumerable TestData { get { // totally doesn’t work return new List() { new object[] { new MsgData() { Code = “1” }, CustomEnum.Value1 }, new […]

在ASP.NET Core MVC API控制器上对AuthorizeAttribute进行unit testing

我有一个ASP.NET核心MVC API与控制器需要进行unit testing。 控制器: using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; namespace TransitApi.Api.Controllers { [Route(“api/foo”)] public class FooController : Controller { private IFooRepository FooRepository { get; } public FooController(IFooRepository fooRepository) { FooRepository = fooRepository; } [HttpGet] [Authorize(“scopes:getfoos”)] public async Task GetAsync() { var foos = await FooRepository.GetAsync(); return Json(foos); } } } 我必须能够对AuthorizeAttribute的有效性进行unit testing。 我们的代码库中存在缺少属性和不正确范围的问题。 这个答案正是我正在寻找的,但在Microsoft.AspNetCore.Mvc.Controller没有ActionInvoker方法意味着我无法这样做。 […]

如何为使用AutoMapper和dependency injection的.net core 2.0服务编写xUnit测试?

我是.net核心/ C#编程的新手(来自Java) 我有以下Service类,它使用dependency injection来获取AutoMapper对象和数据存储库对象,以用于创建SubmissionCategoryViewModel对象的集合: public class SubmissionCategoryService : ISubmissionCategoryService { private readonly IMapper _mapper; private readonly ISubmissionCategoryRepository _submissionCategoryRepository; public SubmissionCategoryService(IMapper mapper, ISubmissionCategoryRepository submissionCategoryRepository) { _mapper = mapper; _submissionCategoryRepository = submissionCategoryRepository; } public List GetSubmissionCategories(int ConferenceId) { List submissionCategoriesViewModelList = _mapper.Map<IEnumerable, List>(_submissionCategoryRepository.GetSubmissionCategories(ConferenceId) ); return submissionCategoriesViewModelList; } } 我正在使用Xunit编写unit testing。 我无法弄清楚如何为方法GetSubmissionCategories编写unit testing,并让我的测试类提供IMapper实现和ISubmissionCategoryRepository实现。 到目前为止,我的研究表明我可以创建依赖对象的测试实现(例如SubmissionCategoryRepositoryForTesting),或者我可以使用模拟库来创建依赖接口的模拟。 但我不知道如何创建AutoMapper的测试实例或AutoMapper的模拟。 如果你知道任何好的在线教程,详细介绍如何创建一个unit testing,其中被测试的类使用AutoMapper和dependency injection一个很好的数据存储库。 […]

使用XUnit和ASP.NET Core 1.0进行dependency injection

我试图找出如何使用XUnit的dependency injection。 我的目标是能够将我的ProductRepository注入我的测试类。 这是我正在尝试的代码: public class DatabaseFixture : IDisposable { private readonly TestServer _server; public DatabaseFixture() { _server = new TestServer(TestServer.CreateBuilder().UseStartup()); } public void Dispose() { // … clean up test data from the database … } } public class MyTests : IClassFixture { DatabaseFixture _fixture; public ICustomerRepository _repository { get; set; } public MyTests(DatabaseFixture […]

使用ReSharper,如何在长时间运行的unit testing中显示调试输出?

我正在使用xUnit和ReSharper测试运行器以及xUnitContrib resharper插件。 当我有一个长时间运行的测试时,我希望能够将一些进度指示器输出到Unit Test Output窗口。 我已经尝试过Debug.WriteLines , Trace.WriteLine和Console.WriteLine 。 所有这些都具有相同的行为 – 在测试完成之前,输出窗口中没有任何内容显示。 例如: [Fact] public void Test() { Debug.WriteLine(“A”); Trace.WriteLine(“B”); Console.WriteLine(“C”); Thread.Sleep(10000); } 在10秒钟结束并且测试完成之前,测试显示没有输出。 如何获得输出? 更新1 我也试过MSTest和NUnit。 NUnit是唯一一个在整个过程中显示输出的人。 在测试完成之前,MSTest和XUnit不会返回任何输出。 奇怪的是,XUnit和NUnit测试输出如下所示: A B C MSTest输出如下所示: C Debug Trace: A B 鉴于所有这些变化,我认为答案是由测试运行器实现决定如何以及何时输出。 有谁知道是否可以配置XUnit测试运行器? 更新2 我认为这必须是xUnitContrib的一个缺陷。 发布到他们的CodePlex问题跟踪器 。

使用Assert.AreEqual()比较两个对象

我是第一次在visual studio中编写测试用例c#我有一个方法返回一个对象列表,我想通过使用Assert.AreEqual()方法将它与另一个对象列表进行比较。 我试过这样做,但即使两个对象相同,断言也会失败。 我想知道这个方法,这两个参数是比较引用还是对象的内容, 我是否必须重载==运算符才能使其工作?