我正在尝试编写一个集成测试,测试我的应用程序的注册功能。这是控制器:
[Route("account")]
public class IdentityController : MainController
{
// ...
[HttpGet("signup")]
public IActionResult SignUp()
{
return View();
}
[HttpPost("signup")]
public async Task<IActionResult> SignUp(UserSignUpViewModel signUp)
{
if (!ModelState.IsValid)
{
我正在跟踪一次在线培训,根据视频以及周围的其他例子,应该这样做才能测试提交的表单:
[Fact]
public async Task Identity_CreateUser_ShouldBeSuccessful()
{
// Arrange
var initialResponse = await _fixture.Client.GetAsync("/account/signup");
initialResponse.EnsureSuccessStatusCode();
var antiForgeryToken = _fixture.GetAntiForgeryToken(await initialResponse.Content.ReadAsStringAsync());
var postRequest = new HttpRequestMessage(HttpMethod.Post, "/account/signup")
{
Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "Name", "John Malone Doe" },
// ...
}
}
// Act
var response = await _fixture.Client.SendAsync(postRequest);
// ...
骑手甚至会为我自动完成这条路,但是它会失败,因为它会发出这样的信息:
System.InvalidOperationException:提供了无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress。
我尝试传递完整地址"https://localhost:5001/account/signup",并以这种方式编写代码:
var postRequest = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://localhost:5001/account/signup"),
Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
没有解决问题。
发布于 2021-12-24 02:16:28
看来URI需要一个尾斜杠才能有效。
请参阅:Create new URI from Base URI and Relative Path - slash makes a difference?
与其使用https://localhost:5001/account/signup
,不如尝试https://localhost:5001/account/signup/
。
(在第二个结尾处有一个斜杠)
不过,我还没测试过这个。
https://stackoverflow.com/questions/70467086
复制相似问题