Skip to content

Instantly share code, notes, and snippets.

@dany1468
Created January 31, 2018 08:09
Show Gist options
  • Select an option

  • Save dany1468/4841beec8109ba84999a7e939e28a066 to your computer and use it in GitHub Desktop.

Select an option

Save dany1468/4841beec8109ba84999a7e939e28a066 to your computer and use it in GitHub Desktop.
ASP.NET Core MVC 2.0 時点での xUnit と Microsoft.AspNetCore.TestHost を利用した Integration Test で気をつける点 ref: https://qiita.com/dany1468/items/ceffb14738342d296839
public class PrimeWebDefaultRequestShould
{
private readonly TestServer _server;
private readonly HttpClient _client;
public PrimeWebDefaultRequestShould()
{
// Arrange
_server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>());
_client = _server.CreateClient();
}
[Fact]
public async Task ReturnHelloWorld()
{
// Act
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal("Hello World!",
responseString);
}
}
var asm = typeof(Startup).Assembly.GetName().Name;
string appRootPath = Path.GetFullPath(Path.Combine(
AppContext.BaseDirectory,
"..", "..", "..", "..", asm));
_server = new TestServer(new WebHostBuilder()
.UseContentRoot(appRootPath) // 💡 これを追加
.UseStartup<Startup>());
<Target Name="CopyAditionalFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
<ItemGroup>
<DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
</ItemGroup>
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
</Target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment