2011-11-23 50 views
10

如何使用Moq创建一个纯存根?随着犀牛嘲笑我这样做是这样的:如何使用Moq创建存根

[TestFixture] 
public class UrlHelperAssetExtensionsTests 
{ 
    private HttpContextBase httpContextBaseStub; 
    private RequestContext requestContext; 
    private UrlHelper urlHelper; 
    private string stylesheetPath = "/Assets/Stylesheets/{0}"; 

    [SetUp] 
    public void SetUp() 
    { 
      httpContextBaseStub = MockRepository.GenerateStub<HttpContextBase>(); 
      requestContext = new RequestContext(httpContextBaseStub, new RouteData()); 
      urlHelper = new UrlHelper(requestContext); 
    } 

    [Test] 
    public void PbeStylesheet_should_return_correct_path_of_stylesheet() 
    { 
     // Arrange 
     string expected = stylesheetPath.FormatWith("stylesheet.css"); 

     // Act 
     string actual = urlHelper.PbeStylesheet(); 

     // Assert 
     Assert.AreEqual(expected, actual); 
    } 
} 

我将如何创建使用起订量为MockRepository.GenerateStub<HttpContextBase>();存根?还是应该留在Rhino Mocks?

回答

1
var mockHttpContext = new Mock<HttpContextBase>(); 
+0

我知道有一个存根和模拟的差异,但您的实现创建一个模拟或存根?看起来像对我的模拟? –

+3

命名是指您使用此对象的方式。所以,如果你不会验证这个对象上的任何东西,它是一个存根,如果你愿意的话 - 这是一个模拟。 – BartoszKP

10

这里是我的建议给你:

Mock<HttpContextBase> mock = new Mock<HttpContextBase>(); 
mock.SetupAllProperties(); 

然后你要做的设置。

如需进一步信息请参阅homepage of the MOQ project.

+0

我需要什么设置?我只需要在我的代码中使用它。我不使用httpContextBaseStub任何地方。 –

+0

你必须以这种方式设置它,你的课堂需要它。这取决于你想运行的单元测试。你可以说一般。 – Fischermaen

+0

我已经更新了我的测试。请检查:) –