English 中文(简体)
Rhino mocks - does this test look sensible?
原标题:

I have just crafted the following test using Rhino mocks. Does my test look valid and make sense to those more experienced with mocking?

I am a a little confused that I haven t had to use the DynamicMock() or StrictMock() methods to create a seemingly valid test.

This test tests that the Add method was invoked on the supplied ICachingStrategy with the supplied parameters.

    object o = new object();
    DateTime d = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, 0, 0, 0);
    CacheStorageStyle s = CacheStorageStyle.Unmodified;
    string f = "test";

    //arrange  
    var stubStrategy = MockRepository.GenerateStub<ICachingStrategy>();
    var stubEncoder = MockRepository.GenerateStub<ICacheItemEncoder>();
    stubStrategy.Stub(x => x.Add(o,d,s,f)).Return("test:key");            
    stubEncoder.Stub(x => x.Encode(o,s)).Return(o);

    _mocks.ReplayAll();

    //act
    ICache c = new Cache(stubStrategy, stubEncoder);
    c.Add(o, d, s, f);

    //assert
    stubStrategy.AssertWasCalled(x => x.Add(o,d,s,f)); 
最佳回答

The question is: does it succeed? Do you expect it to? If the answers are yes and yes, then the test is good. You can further test the test by forcing it to fail by commening out the call to Add() in your implementation. If the Add() method is not called, the test should fail.

Here is the relevant documentation that explains the difference between stubs and mocks. The essential difference is that stubs "will never cause a test to fail." They are there just to make the testing code work. The documentation further recommends that stubs should be preferred over mocks wherever possible.

Your test appears valid to me because you are primarily interested in whether the Add() method was called, and you are explicitly asserting for that call. You re not testing the other method calls, so you re stubbing them instead of mocking them.

问题回答

re: Strict vs Dynamic - another things thats worth noting is that in RhinoMocks 3.5 a call to MockRepository.GenerateMock() will not generate a strict mock.

Depending on what your testing you may/may not care what was/wasn t called on your mock.





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签