我正在运行一些集成测试,我希望在ASPNETUsers
表中创建一个“假”用户。
我试图在没有运气的情况下做以下事情。
所以我的设置如下: -
[SetUp]
public void Setup()
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var projectPath = Path.GetFullPath(Path.Combine(basePath, "../../../../SportsStore2.Tests"));
var server = new TestServer(Utils.GetHostBuilder(new string[] { })
.UseContentRoot(projectPath)
.UseEnvironment("Development")
.UseStartup<Startup>());
_client = server.CreateClient();
}
然后插入如下: -
var registerViewModel = new RegisterViewModel
{
Email = "test@test.com",
ConfirmPassword = "testing12345?",
Password = "testing12345?"
};
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var postResponseASPNETUser = await _client.PostAsJsonAsync("http://localhost:49406/Account/Register", registerViewModel);
var createdASPNETUser = await postResponseASPNETUser.Content.ReadAsStringAsync();
但是我收到了400回复,错误请求。
任何想法我做错了什么?
感谢您的帮助和时间。
答案 0 :(得分:0)
不是直接调用api,因为我没有运气而且找不到为什么它没有返回正确的响应,我决定走另一条路线,基本上在我的UsersController中创建了另一个Create Action,如下所示: -
[HttpPost("Create")]
public IActionResult Create([FromBody] User user)
{
if (user == null)
return BadRequest();
var result = _usersService.Add(user, m => m.Name == user.Name);
if (result)
{
return CreatedAtRoute("GetUsers", new { id = user.Id }, user);
}
return BadRequest("Item not added");
}
[HttpPost("CreateAspNetUsers")]
public IActionResult CreateAspNetUsers([FromBody] RegisterViewModel registerViewModel)
{
if (registerViewModel == null)
return BadRequest();
var result = _aspNetUsersService.Add(registerViewModel, m => m.Name == registerViewModel.Email);
if (result)
{
return CreatedAtRoute("GetUsers", registerViewModel);
}
return BadRequest("Item not added");
}
这很好,并且给了我我追求的结果。