简体   繁体   中英

Create “fake” user in my ASPNETUsers Table

I am running some Integration Tests and I wish to create a "fake" user in my ASPNETUsers table.

I am trying to do the following with no luck.

So I have the Setup as follows :-

[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();
}

and then the Insert is as follows :-

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();

However I am getting a 400 response, Bad Request.

Any ideas what I am doing wrong?

Thanks for your help and time.

Instead of calling the api directly, since I had no luck and could not find why it was not returning a correct response, I decided to go another route, and basically created another Create Action inside my UsersController as follows:-

    [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");
    }

This works fine and is giving me the result I was after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM