gpt4 book ai didi

c# - 如何使用 Moq 创建 stub

转载 作者:可可西里 更新时间:2023-11-01 07:45:13 26 4
gpt4 key购买 nike

如何使用 Moq 创建纯 stub ?使用 Rhino Mocks,我是这样做的:

[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>(); 创建 stub 使用最小起订量?还是我应该继续使用 Rhino Mocks?

最佳答案

这是我给你的建议:

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

然后您必须进行设置。

有关更多信息,请参阅 homepage of the MOQ project.

关于c# - 如何使用 Moq 创建 stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8241887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com