Bre*_*ogt 15 c# nunit rhino-mocks moq asp.net-mvc-3
如何使用Moq创建纯存根?使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
如何MockRepository.GenerateStub<HttpContextBase>();使用Moq 创建存根?或者我应该留在Rhino Mocks?
Fis*_*aen 13
以下是我对你的建议:
Mock<HttpContextBase> mock = new Mock<HttpContextBase>();
mock.SetupAllProperties();
Run Code Online (Sandbox Code Playgroud)
然后你必须进行设置.
有关更多信息,请参阅MOQ项目的主页.
这里的派对有点晚了,但在我看来,这里仍然没有足够的答案.
Moq没有明确的存根和模拟生成,就像RhinoMocks一样.相反,所有设置调用,例如mockObject.Setup(x => blah ...)创建存根.
但是,如果您希望将相同的代码视为模拟,则需要调用mockObject.Verify(x => blah ...)断言设置按预期运行.
如果你打电话mockObject.VerifyAll(),它会将你设置的所有内容视为模拟,这不太可能是你想要的行为,即所有存根被视为模拟.
相反,在设置模拟时,使用该mockObject.Setup(x => blah ...).Verifiable()方法将设置明确标记为模拟.然后调用mockObject.Verify()- 然后只断言已标记的设置Verifiable().
var mockHttpContext = new Mock<HttpContextBase>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14027 次 |
| 最近记录: |