xunit

How to parametrize a xunit class fixture?

别等时光非礼了梦想. 提交于 2021-02-20 18:42:29
问题 xUnit offers the concept of (shared) class fixtures as explained in Shared Context between Tests. What I didn't figure out so far is if there is a way of parametrizing such class fixtures. For example, what if the DatabaseFixture should be enriched with some test data which depends on the test it's being run against? A test class might want to insert test data but only once and then run all its tests against that database (fixture). In other words, what if the // ... initialize data in the

Are there Parameterized Test Fixtures for xunit?

风格不统一 提交于 2021-02-18 22:15:45
问题 Does xunit.net support "Parameterized Test Fixtures" like nunit (see example code below). Note I'm not looking for IUseFixture<T> or [Theory] because these do not provide the same functionality as Parameterized Test Fixtures [TestFixture("hello", "hello", "goodbye")] [TestFixture("zip", "zip")] [TestFixture(42, 42, 99)] public class ParameterizedTestFixture { private string eq1; private string eq2; private string neq; public ParameterizedTestFixture(string eq1, string eq2, string neq) { this

Trying to Test a Controller with a UrlHelper

▼魔方 西西 提交于 2021-02-16 18:07:17
问题 Trying to create a URLHelper for testing purposes throws a NullReferenceException . Example: [Fact] public async void AuthenticateAsyncTest() { // Arrange var controller = new Controller(serviceProvider) { Url = new UrlHelper(new ActionContext()) // Exception thrown }; // Act var result = await controller.Authenticate() as ViewResult; // Assert Assert.NotNull(result); } Every time I run this Test, the Exception that is thrown in Url = new UrlHelper(new ActionContext()) is: Exception.Message:

A kind of integration testing in ASP.NET Core, with EF and AutoMapper

对着背影说爱祢 提交于 2021-02-11 14:32:29
问题 I'm trying to test the path from my ASP.NET Core Controller to the DB, through repository which includes the usage of AutoMapper. Here's my repository: using System; using System.Linq; using AutoMapper; using DS.DTO.MasterData; using DS.Utilities.DSExceptions; using Microsoft.Extensions.Logging; using Omu.ValueInjecter; namespace DS.MasterData.Repositories { public class PersonFactRepository : IPersonFactRepository { private readonly Database.MasterDataContext dbContext; private readonly

Callback of Moq is not called?

做~自己de王妃 提交于 2021-02-11 13:41:02
问题 In the following code. The Assert.Equal(...) of the Callback() is never called? var test = "Test"; var command = new MyCommand { V = test }; var mock = new Mock<IRepository>(); // IRepository has the method of Save() var p = new P(test); mock.Setup(x => x.Save(p)) .Callback<P>(x => Assert.Equal(x.Value, test)); // break point on Assert.Equal not hit var sut = new C(mock.Object); var result = await sut.M(command); 回答1: You have: .Setup(x => x.Save(p)) but are you sure the P used in your SUT is

How to use MassTransit test harness to test Consumer with constructor dependency injection?

陌路散爱 提交于 2021-02-08 15:14:23
问题 I have some message consumers that take dependencies through the constructor, and I would like to cover them in unit tests. Does MassTransit's test harness provide a way to register consumers with constructor parameters? 回答1: You can specify a factory method, or a consumer factory, when creating a consumer test harness. harness.Consumer<T>(() => new T()); You can find the three standard extension methods in the code: https://github.com/MassTransit/MassTransit/blob/develop/src/MassTransit

xunit - how to get HttpContext.User.Identity in unit tests

拜拜、爱过 提交于 2021-02-08 14:01:12
问题 I added a method to my controllers to get the user-id from the JWT token in the HttpContext . In my unit tests the HttpContext is null , so I get an exception. How can I solve the problem? Is there a way to moq the HttpContext ? Here is the method to get the user in my base controller protected string GetUserId() { if (HttpContext.User.Identity is ClaimsIdentity identity) { IEnumerable<Claim> claims = identity.Claims; return claims.ToList()[0].Value; } return ""; } One of my tests look like

xunit - how to get HttpContext.User.Identity in unit tests

笑着哭i 提交于 2021-02-08 14:01:04
问题 I added a method to my controllers to get the user-id from the JWT token in the HttpContext . In my unit tests the HttpContext is null , so I get an exception. How can I solve the problem? Is there a way to moq the HttpContext ? Here is the method to get the user in my base controller protected string GetUserId() { if (HttpContext.User.Identity is ClaimsIdentity identity) { IEnumerable<Claim> claims = identity.Claims; return claims.ToList()[0].Value; } return ""; } One of my tests look like

Deploy project with xUnit reference as NuGet package

久未见 提交于 2021-02-07 17:29:50
问题 I have a library that contains some classes, which I need in several Unit Test projects. The library should be deployed as a NuGet package in my private repository. I already deployed some NuGet packages there, so I know what I have to do. BUT: Inside of this library I need a reference to xUnit. And as soon as I add this reference, there is no more .nupkg file created when execute dotnet pack . Another interesting effect is, that the project icon turns into a Unit Test icon as soon as I add

Deploy project with xUnit reference as NuGet package

风格不统一 提交于 2021-02-07 17:29:41
问题 I have a library that contains some classes, which I need in several Unit Test projects. The library should be deployed as a NuGet package in my private repository. I already deployed some NuGet packages there, so I know what I have to do. BUT: Inside of this library I need a reference to xUnit. And as soon as I add this reference, there is no more .nupkg file created when execute dotnet pack . Another interesting effect is, that the project icon turns into a Unit Test icon as soon as I add