2016-07-29 57 views
1

我真的很困惑。所以我一直在研究一個Web API和單元測試項目,並且最近剛剛對很多控制器做了一些改動,但完全沒有改變AccountController。在我的AccountController中,我有Register()這個函數,在我測試的時候,這個函數在過去的2個月裏一直很棒。現在突然雖然當我進入註冊和呼叫,UserManager.CreateAsync掛起執行單元測試但不是郵差

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

所以沒有這些功能已經改變了所有,我的猜測是它做的await的東西,但我就是想不通爲什麼它正在以這種方式行事。

+0

道歉詢問顯而易見,但當你通過單元測試時會發生什麼?你應該能夠很快找出它掛起的位置。在調試模式下表示客戶端和服務器。 – Rake36

+0

我已經通過了整個單元測試,沒有掛在那裏,但它掛在通話服務器端, IdentityResult result = await UserManager.CreateAsync(user,model.Password); – tokyo0709

回答

1

剛剛解決了這個問題。我的電腦和Visual Studio完全重新啓動,瞧。在一個側面說明,任何人都知道爲什麼這解決了這個問題?也許只是一個Visual Studio的錯誤?

+0

通常掛在異步代碼是因爲你沒有使用異步,在調用棧中混合使用'Task .Result'和'await'。但是,這不會通過重新啓動VS來解決,因此您可能遇到了VS中的錯誤? – Igor

+0

我遇到了同樣的問題,但即使是重新啓動也沒有幫助:/ 在我的情況下,我甚至可以在調試中運行所有測試時看到掛起。我可以踩到CreateAsync並讓它掛在我身上。 – Sam