馬特·李奇微的答案寫,當RC1是當前版本。現在(RTM 1.0.0/1.0.1),這已經變得簡單:
public class TenantTests
{
private readonly TestServer _server;
private readonly HttpClient _client;
public TenantTests()
{
_server = new TestServer(new WebHostBuilder()
.UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "..", "..", "..", "SaaSDemo.Web")))
.UseEnvironment("Development")
.UseStartup<Startup>());
_client = _server.CreateClient();
}
[Fact]
public async Task DefaultPageExists()
{
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
Assert.True(!string.IsNullOrEmpty(responseString));
}
}
關鍵的一點是這裏.UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "..", "..", "..", "SaaSDemo.Web")))
的ApplicationBasePath是在您的測試組件斌/調試/ {平臺版本}/{os-buildarchitecture} /文件夾。您需要向上遍歷該樹,直到您到達包含視圖的項目。就我而言。 SaasDemo.Tests
與SaasDemo.Web
位於同一個文件夾中,因此遍歷5個文件夾的數量是正確的。
您是否找到此解決方案 –
不需要,我仍然需要一個解決方案:( – Sam