2011-12-20 103 views
1

我在理解MVC的單元測試時遇到了一些問題。我有我的模型它看起來像這樣:MVC 3測試模式和控件的單元測試

namespace UnifiedLibrary.Models 
{ 
    public class SCPublisherModel 
    { 
     [Display(Name = "Title")] 
     public string Title { get; set; } 

     [DataType(DataType.MultilineText)] 
     [Display(Name = "Description")] 
     public string Description { get; set; } 

     public bool Conform() 
     { 
      if (String.IsNullOrEmpty(Title)) 
       Title = Resources.ULResLocal.Sou.locDefaultTitle; 

      if (String.IsNullOrEmpty(Description)) 
      { 
       Description = Resources.ULResLocal.Sou.locDefaultDesc; 

      } 
      return true; 
     } 

     public bool Validate() 
     { 
      if (String.IsNullOrEmpty(Title)) 
       Title = Resources.ULResLocal.Sou.locDefaultTitle; 

      return true; 
     } 


    } 
} 


    public ActionResult Sou(string productID, string locale, string clienttoken, string Title, string Description) 
     { 
      ProviderID = (int)Providers.SoundCloud; 

      locale = Utilities.SetApplicationLocale(locale); 

      if (String.IsNullOrEmpty(clienttoken)) 
       return RedirectToAction("../Shared/Error"); 

       HttpCookie cookieAuthToken = this.ControllerContext.HttpContext.Request.Cookies[strCookieSoundCloudAuthToken]; 
     if (cookieAuthToken == null) 
      { 
       return RedirectToAction("../Login/LoginSoundCloud", new { productID = productID, locale = locale, clienttoken = clienttoken, 
        Title = Title, Description = Description}); 
      } 

      // create a model for soundcloud and store all known information 
      Models.SCPublisherModel model = new Models.SCPublisherModel(); 
      model.Title = Title; 
      model.Description = Description; 
      model.Conform(); 



      // create a view based on the updated model 
      return View(model); 
     } 

我也需要檢查這個控制。但我不知道如何。我已經使用了這個單元測試,但我得到了錯誤。

[TestMethod] 
     public void SoundCloudTest() 
     { 
      // Controllers that rely on App_GlobalResources or App_Local_Resources cannot be unit tested with rewriting these dependencies. 
      var controller = new PublishController(); 
      // Act 
      var SCModelTest = new SCPublisherModel() 
      { 
       Title = "Test 100", 

      }; 

      var result = controller.SoundCloud(SCModelTest) as ViewResult; 


      Assert.AreEqual(result.View, SCModelTest); 

      } 

如果您知道任何文章。你能把它提供給我嗎? 謝謝。

回答

0

變化

Assert.AreEqual(result.View, SCModelTest);

到:

Assert.AreEqual(result.Model, SCModelTest);