xunit

How to unit test a Web API controller using XUnit

六月ゝ 毕业季﹏ 提交于 2020-05-30 07:44:26
问题 I am trying to unit test a method within my controller in my Web API using XUnit. The role of the method is to get a single title, by ISBN, from the database. The issue I came across during unit testing is that I am unsure how to insert the dummy data that I must perform the test on, as well as how the Assert function works. TitleController.cs [ApiController] [Route("titlecontroller")] public class TitleController : Controller { private IGtlTitleRepository _gtlTitleRepository; public

Populate IConfiguration for unit tests

狂风中的少年 提交于 2020-05-28 14:18:06
问题 .NET Core configuration allows so many options to add values (environment variables, json files, command line args). I just can't figure out and find an answer how to populate it via code. I am writing unit tests for extension methods to configurations and I thought populating it in the unit tests via code would be easier than loading dedicated json files for each test. My current code: [Fact] public void Test_IsConfigured_Positive() { // test against this configuration IConfiguration config

Static member error with instance member, xUnit

不打扰是莪最后的温柔 提交于 2020-04-16 02:28:31
问题 "An object reference is required for the non-static field" I have a static member which cannot be an instance member. This static member uses an instance of an object that is initialized in the constructor of the class through dependency injection, so it cannot be static. How could I solve this error? public class My { private readonly Fixture _fixture; public MyClass(Fixture fixture) { _fixture = fixture; } public static IEnumerable<object[]> TestCases() { yield return new object[] { Urls

Combining 2 reports into one 1 in (testng) allure

ε祈祈猫儿з 提交于 2020-04-14 07:19:39
问题 My requirement is to combine 2 test results to publish an allure report. Basically, our framework needs run 2 exclusive sets of related tests bases on system status as 2 different test runs. These will run from 2 different JVMs. I need to combine the result of these 2 runs to show 1 report. As allure provides an adapter for testng, I'm wondering if this is possible or where should I start. Thanks in advance for all the guidance. 回答1: Allure has built-in aggregation functionality, just put two

Combining 2 reports into one 1 in (testng) allure

偶尔善良 提交于 2020-04-14 07:19:08
问题 My requirement is to combine 2 test results to publish an allure report. Basically, our framework needs run 2 exclusive sets of related tests bases on system status as 2 different test runs. These will run from 2 different JVMs. I need to combine the result of these 2 runs to show 1 report. As allure provides an adapter for testng, I'm wondering if this is possible or where should I start. Thanks in advance for all the guidance. 回答1: Allure has built-in aggregation functionality, just put two

How to unit test ActionFilterAttribute

半世苍凉 提交于 2020-04-05 17:48:32
问题 I'm looking to test an ActionFilterAttribute in a .NET Core 2.0 API project and wondering the best way to go about it. Note, I'm not trying to test this through a controller action, merely test the ActionFilterAttribute itself. How might I go about testing this: public class ValidateModelAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { context.Result = new BadRequestObjectResult(context.ModelState);

How to unit test ActionFilterAttribute's OnActionExecuting() with Xunit [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-25 12:32:41
问题 This question already has answers here : How to unit test ActionFilterAttribute (1 answer) How to mock ActionExecutingContext with Moq? (2 answers) Closed 5 days ago . This is in ASP.net core 3.1 , using XUnit and NSubstitute (Limited to these frameworks by business rules) I have a ActionFilterAttribute derived model validation filter, (taken from this article: Validate the Model State Globally in .NET Core), that I need to unit test. I'm having trouble getting the tests to work because I am

net core 2.0 xunit Controller with dependency injection using Mock is null

試著忘記壹切 提交于 2020-03-25 12:32:14
问题 I am doing an Net Core 2.0 Rest Api application. I am using dependency injection with Interface IContactBiz and class ContactBiz . IContactBiz is defined like this public interface IContactBiz { ReturnModel Add(List<ContactEntity> lstContactEntity, ContactEntity contact); } My ContactBiz class public class ContactBiz: IContactBiz { public ReturnModel Add(List<ContactEntity> lstContactEntity, ContactEntity contact) { contact.ID = Guid.NewGuid().ToString(); lstContactEntity.Add(contact); return

How to unit test ActionFilterAttribute's OnActionExecuting() with Xunit [duplicate]

我与影子孤独终老i 提交于 2020-03-25 12:32:02
问题 This question already has answers here : How to unit test ActionFilterAttribute (1 answer) How to mock ActionExecutingContext with Moq? (2 answers) Closed 5 days ago . This is in ASP.net core 3.1 , using XUnit and NSubstitute (Limited to these frameworks by business rules) I have a ActionFilterAttribute derived model validation filter, (taken from this article: Validate the Model State Globally in .NET Core), that I need to unit test. I'm having trouble getting the tests to work because I am

Get name of running test in Xunit

回眸只為那壹抹淺笑 提交于 2020-03-18 03:26:26
问题 Using Xunit, how can I get the name of the currently running test? public class TestWithCommonSetupAndTearDown : IDisposable { public TestWithCommonSetupAndTearDown () { var nameOfRunningTest = "TODO"; Console.WriteLine ("Setup for test '{0}.'", nameOfRunningTest); } [Fact] public void Blub () { } public void Dispose () { var nameOfRunningTest = "TODO"; Console.WriteLine ("TearDown for test '{0}.'", nameOfRunningTest); } } Edit: In particular, I am looking for a replacement for NUnits