UserManager.CreateAsync在单元测试执行时挂起但不挂起Postman

时间:2016-07-29 16:56:30

标签: c# asp.net authentication asp.net-identity

我现在真的很困惑。因此,我一直在研究Web API并对单元进行单元测试,并且最近刚刚对很多控制器进行了更改,但完全没有触及AccountController。在我的AccountController中,我有一个函数Register(),它在我每次测试时都在过去2个月里运行良好。现在突然间,当我进入注册并致电时,

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

我的程序挂起,不会再回来。我测试了一些东西,这只发生在从我的单元测试调用时(也没有改变),当我从Postman调用Register时使用完全相同的JSON它可以正常工作。

注册API函数

// POST api/Account
[AllowAnonymous]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, Active = true, CreationDate = DateTime.Now };

    IdentityResult result = await UserManager.CreateAsync(user, model.Password);

    if (!result.Succeeded)
    {
        return GetErrorResult(result);
    }

    return Ok();
}

测试功能

[TestMethod]
public void Register_Pass()
{
    // Arrange & Act
    var db = new WizardSwearsDB();

    new DBCleanup().RemoveRegisteredUser(userName);
    string apiCall = "";
    new
    {
        UserName = userName,
        Email = email,
        Password = password,
        ConfirmPassword = password
    }.Post(baseUrl + apiCall);

    var user = (from u in db.AspNetUsers
                where u.UserName == userName
                select u).FirstOrDefault();

    // Assert
    Assert.IsNotNull(user);
    Assert.AreEqual(userName, user.UserName);
}

JSON帖子功能

public static void Post(this object obj, string url, string securityToken = null)
{
    var request = WebRequest.Create(url);
    obj.Request<string>(url, postJsonRequestBuilder, null, securityToken);
}

private static Func<string, object, string, WebRequest> postJsonRequestBuilder = (url, o, st) => jsonRequestBuilder(url, o, "POST", st);

private static Func<string, object, string, string, WebRequest> jsonRequestBuilder = delegate(string url, object obj, string method, string securityToken)
{
    WebRequest request = WebRequest.Create(url);
    if (!string.IsNullOrEmpty(securityToken)) request.Headers.Add("Authorization", string.Format("Bearer {0}", securityToken));
    request.Method = method;
    if (obj != null)
    {
        request.ContentType = "application/json";
        var payload = encoding.GetBytes(JsonConvert.SerializeObject(obj));
        request.ContentLength = payload.Length;
        using (var stream = request.GetRequestStream())
        {
            stream.Write(payload, 0, payload.Length);
        }
    }
    return request;
};

删除注册用户功能并删除员工功能

public void RemoveRegisteredUser(string userName)
{
    // Open up a connection to the database
    var db = new WizardSwearsDB();

    // Get the user from the ASPNetUsers table
    var dbUserASP = (from b in db.AspNetUsers
                    where b.UserName == userName
                    select b).FirstOrDefault();


    // Remove the user from both tables and save changes
    if (dbUserASP != null)
    {
        db.AspNetUsers.Remove(dbUserASP);
        db.SaveChanges();
    }

    RemoveEmployee(userName);
}

public void RemoveEmployee(string userName)
{
    // Open up a connection to the database
    var db = new WizardSwearsDB();

    // Get the user from the ASPNetUsers table
    var employee = (from b in db.Employees
                    where b.UserName == userName
                    select b).FirstOrDefault();

    // Remove the user from both tables and save changes
    if (employee != null)
    {
        db.Employees.Remove(employee);
        db.SaveChanges();
    }
}

所以这些功能都没有改变,我的猜测是它必须做等待的事情,但我无法弄清楚它为什么会这样做。

2 个答案:

答案 0 :(得分:1)

刚刚解决了问题。我的电脑和Visual Studio完全重启了瞧。在旁注,任何人都熟悉为什么解决了这个问题?也许只是一个Visual Studio错误?

答案 1 :(得分:0)

尝试使用现有用户名创建用户时,我遇到了同样的问题-没有错误,没有任何内容,为空白。