Should we use ControllerName.Whatever in place of [Route("[controller]")]? #216
Replies: 12 comments
-
If it is an integration test using the ASP.NET Core |
Beta Was this translation helpful? Give feedback.
-
BTW, feel free to reopen any issues, where you feel I have not answered everything. I'm just trying to limit the number of open issues. |
Beta Was this translation helpful? Give feedback.
-
Good point, I am using the new Core WebApplicationFactory and |
Beta Was this translation helpful? Give feedback.
-
Here is an example of how to resolve instances:
You can resolve an instance of |
Beta Was this translation helpful? Give feedback.
-
Hmm so I am not sure if this helps entirely. In Core 2.1 you can only resolve a IUrlHelperFactory and from there you can call GetUrlHelper, but the problem is you can not do that without an ActionContext. Since there is no action context there is no UrlHelper. This is my current code: public class SmokeTests : IClassFixture<CustomConfigurationWebApplicationFactory<MyApplication.API.Startup>>
{
private readonly CustomConfigurationWebApplicationFactory<MyApplication.API.Startup> _factory;
private const string urlPrefix = "/api/";
public SmokeTests(CustomConfigurationWebApplicationFactory<MyApplication.API.Startup> factory)
{
_factory = factory;
}
[Theory]
[InlineData(ControllerName.Contact)]
[InlineData(ControllerName.Location)]
[InlineData("LifecycleMaturitie")]
public async Task Get_EndpointsReturn_SuccessfulCode_CorrectContentType_SomeData(string controllerName)
{
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetAsync($"{urlPrefix}{controllerName}s/1");
// Assert
var json = await response.Content.ReadAsStringAsync();
var dyn = JObject.Parse(json);
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.Equal("application/json; charset=utf-8",
response.Content.Headers.ContentType.ToString());
Assert.Equal(1, dyn["Id"]);
}
} You can see some really bad code here where I am trying to essentially glue an "s" to my ControllerNames because I have no way to strongly type out a path to an action in my tests where I use the WebApplicationFactory. |
Beta Was this translation helpful? Give feedback.
-
You want:
|
Beta Was this translation helpful? Give feedback.
-
I might be missing something, after registering that I am not sure how I would retrieve an instance of that |
Beta Was this translation helpful? Give feedback.
-
Take a look at the API project template, I use |
Beta Was this translation helpful? Give feedback.
-
@RehanSaeed I noticed you store the How exactly are you making sure the context is disposed between your tests? I just ran into an issue where I had multiple tests fail because of one single test. This is because XUnit is running all tests in my collection and I am using the same undisposed context between tests which is not good. You cannot do a using() statement because you will dispose the context and there is no "scope" because there technically is no real requests happening right? Are you perhaps running each test with a fresh WebApplicationFactory? |
Beta Was this translation helpful? Give feedback.
-
This might help someone, also id like your thoughts on this. The magic here is [Collection("Master Test Host Collection")]
public class InventoryTests
{
private readonly CustomConfigurationWebApplicationFactoryFixture<WuitCMDB.API.Startup> _factory;
public InventoryTests(CustomConfigurationWebApplicationFactoryFixture<WuitCMDB.API.Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task Delete_Success()
{
// Arrange
var client = _factory.CreateDefaultClient();
MyApiClient _api = RestClient.For<MyApiClient>(client);
var inventorys = default(List<Inventory>);
// Resolve a scoped service within the application scope (Host is the TestHost)
using (var scope = _factory.Server.Host.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<WuitCMDBDbContext>();
inventorys = new List<Inventory>()
{
new Inventory()
{
Name = "Inventory to Delete",
Description = "Inventory to Delete",
ManagedById = 1
},
new Inventory()
{
Name = "Inventory to Delete 2",
Description = "Inventory to Delete",
ManagedById = 1
}
};
context.Inventory.AddRange(inventorys);
context.SaveChanges();
var inventoryIds = inventorys.Select(x => x.Id);
// Act
await _api.DeleteInventoryAsync(String.Join(',', inventoryIds));
// Assert
var itemsStillExist = context.Inventory.Any(x => inventoryIds.Contains(x.Id));
Assert.False(itemsStillExist);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
The Each of my test classes uses a |
Beta Was this translation helpful? Give feedback.
-
Wondering about doing this in my own project. I am writing some tests and it would be nice to do the following
Constants.cs
Tests.cs
Instead I have this
Beta Was this translation helpful? Give feedback.
All reactions