2010-04-22 24 views

回答

8
// Arrange 
var c = new MyController(); 

//Act 
var result = c.Index(null,null); 
var model = result.ViewData.Model; 

//Assert 
Assert("model is what you want"); 

善良, 丹

+1

它在可視數據的屬性!謝謝,沒有看到那裏。 – Michel 2010-04-22 12:52:52

4

我建議你優秀MVContrib test helper。您的測試可能是這樣的:

[TestMethod] 
public void SomeTest() 
{ 
    // arrange 
    var p1 = "foo"; 
    var p2 = "bar"; 

    // act 
    var actual = controller.Index(p1, p2); 

    // assert 
    actual 
     .AssertViewRendered() // make sure the right view has been returned 
     .WithViewData<SomeViewData>(); // make sure the view data is of correct type 
} 

您也可以斷言該模型

actual 
    .AssertViewRendered() 
    .WithViewData<SomeViewData>() 
    .SomeProp 
    .ShouldEqual("some expected value"); 
+0

而不是'.ShouldEqual(「某個期待值」);' 是否意味着'.ShouldBe(「某個期望值」);'? – Muflix 2017-05-09 20:28:13

相關問題