xunit

Unit testing an AuthorizeAttribute on an ASP.NET Core MVC API controller

帅比萌擦擦* 提交于 2019-11-30 18:07:09
I have a ASP.NET Core MVC API with controllers that need to be unit tested. Controller: 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<IActionResult> GetAsync() { var foos = await FooRepository.GetAsync(); return Json(foos); } } } It is essential that I

.Net core library: How to test private methods using xUnit

陌路散爱 提交于 2019-11-30 15:27:40
The latest xunit framework does not allow test runners in library code when compiled with .Net Core framework (this is allowed for normal Visual Studio code). The solution is to create a separate testing project to run your test cases. The problem that I am having is that some methods that I want to test are 'private' methods. How can I call those methods from my test project without making their scope 'public'? The more general question is: How do you test private methods when the public method that uses the private method can not be used? (because of its interactive nature - that is, it has

Comparing Two objects using Assert.AreEqual()

强颜欢笑 提交于 2019-11-30 08:07:36
I 'm writing test cases for the first time in visual studio c# i have a method that returns a list of objects and i want to compare it with another list of objects by using the Assert.AreEqual() method. I tried doing this but the assertion fails even if the two objects are identical. I wanted to know if this method, the two parameters are comparing references or the content of the object, Do I have to overload the == operator to make this work? anouar.bagari If you are using NUnit this is what the documentation says Starting with version 2.2, special provision is also made for comparing single

In F# how do you pass a collection to xUnit's InlineData attribute

霸气de小男生 提交于 2019-11-30 03:29:38
问题 I would like to be about to use a list, array, and/or seq as a parameter to xUnit's InlineData. In C# I can do this: using Xunit; //2.1.0 namespace CsTests { public class Tests { [Theory] [InlineData(new[] {1, 2})] public void GivenCollectionItMustPassItToTest(int[] coll) { Assert.Equal(coll, coll); } } } In F# I have this: namespace XunitTests module Tests = open Xunit //2.1.0 [<Theory>] [<InlineData(8)>] [<InlineData(42)>] let ``given a value it must give it to the test`` (value : int) =

Unit testing an AuthorizeAttribute on an ASP.NET Core MVC API controller

自闭症网瘾萝莉.ら 提交于 2019-11-30 02:15:05
问题 I have a ASP.NET Core MVC API with controllers that need to be unit tested. Controller: 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<IActionResult>

Is it idiomatic Ruby to add an assert( ) method to Ruby's Kernel class?

廉价感情. 提交于 2019-11-30 01:39:44
I'm expanding my Ruby understanding by coding an equivalent of Kent Beck's xUnit in Ruby. Python (which Kent writes in) has an assert() method in the language which is used extensively. Ruby does not. I think it should be easy to add this but is Kernel the right place to put it? BTW, I know of the existence of the various Unit frameworks in Ruby - this is an exercise to learn the Ruby idioms, rather than to "get something done". Julik No it's not a best practice. The best analogy to assert() in Ruby is just raising raise "This is wrong" unless expr and you can implement your own exceptions if

How to get content value in Xunit when result returned in IActionResult type

六月ゝ 毕业季﹏ 提交于 2019-11-29 11:08:30
问题 I have a unit test project using Xunit and the method we are testing returns IActionResult . I saw some people suggest using "NegotiatedContentResult" to get the content of the IActionResult but that doesn't work in Xunit. So I wonder how to get the content value of an IActionResult in Xunit? Test code example is provided below: public void GetTest() { var getTest = new ResourcesController(mockDb); var result = getTest.Get("1"); //Here I want to convert the result to my model called Resource

Can't get XUnit tests to run with .NET Core

随声附和 提交于 2019-11-29 02:07:12
I'm porting a small library that I have on NuGet to .NET Core. I've created .NET Standard 1.6 class libraries for the main project and the tests, and copied over the code. I changed the unit tests to use XUnit attributes and asserts rather than NUnit. Other than that I pretty much followed the instructions in the documentation , thus I added the following NuGet packages: Microsoft.NET.Test.Sdk xunit xunit.runner.visualstudio Alas, (1) Test Explorer does not find my unit tests, and (2) when I run dotnet test , I get the following: Starting test execution, please wait... Could not find testhost

Is it idiomatic Ruby to add an assert( ) method to Ruby's Kernel class?

喜欢而已 提交于 2019-11-28 21:47:43
问题 I'm expanding my Ruby understanding by coding an equivalent of Kent Beck's xUnit in Ruby. Python (which Kent writes in) has an assert() method in the language which is used extensively. Ruby does not. I think it should be easy to add this but is Kernel the right place to put it? BTW, I know of the existence of the various Unit frameworks in Ruby - this is an exercise to learn the Ruby idioms, rather than to "get something done". 回答1: No it's not a best practice. The best analogy to assert()

How to mock ActionExecutingContext with Moq?

痴心易碎 提交于 2019-11-28 21:32:36
I am trying to test the following filter: using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Filters; namespace Hello { public class ValidationFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.ModelState.IsValid) { filterContext.Result = new BadRequestObjectResult(filterContext.ModelState); } } } } I am trying to mock the ActionFilterAttribute using Moq. I am using Mvc 6.0.0-rc1 My first try: var context = new ActionExecutingContext( new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new