Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm a little confused by your test since you are not really testing anything except that RhinoMocks works. You create two mocks and then do some assertions on them. You haven't even tested your real classes.</p> <p>You need to do some dependency injection if you really want to get a good unit test. You can quickly refactor your code to use interfaces and dependency injection to make your test valid.</p> <p>Start by extracting an interface from your OrderParser class:</p> <pre><code>public interface IOrderParser { String ParseOrder(String value); } </code></pre> <p>Now make sure your OrderParser class implements that interface:</p> <pre><code>public class OrderParser: IOrderParser{ ... } </code></pre> <p>You can now refactor your OrderProcessor class to take in an instance of an IOrderParser object through its constructor. In this way you "inject" the dependency into the class.</p> <pre><code>public class OrderProcessor { IOrderParser _orderParser; public OrderProcessor(IOrderParser orderParser) { _orderParser = orderParser; } public virtual string PlaceOrder(string val) { string tester = _orderParser.ParseOrder(val); return tester + " here" ; } } </code></pre> <p>In your test you only want to mock out the dependency and not the SUT (Subject Under Test). Your test would look something like this:</p> <pre><code> public class OrderTest { public void TestParser() { // Arrange var spec = MockRepository.GenerateMock&lt;IOrderParser&gt;(); var client = new OrderProcessor(spec); spec.Stub(x =&gt; x.ParseOrder("test")).IgnoreArguments().Return("Test1"); //Act var s = client.PlaceOrder("Blah"); //Assert Assert.AreEqual("Test1 Here", s); } } </code></pre> <p>It is difficult for me to gauge what you are trying to do with your classes, but you should be able to get the idea from this. A few axioms to follow:</p> <ol> <li>Use interfaces and composition over inheritance</li> <li>Use <a href="http://martinfowler.com/articles/injection.html" rel="nofollow noreferrer">dependency injection</a> for external dependencies (inversion of control)</li> <li>Test a single unit, and mock its dependencies</li> <li>Only mock one level of dependencies. If you are testing class X which depends on Y which depends on Z, you should only be mocking Y and never Z.</li> <li>Always <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development" rel="nofollow noreferrer">test behavior</a> and never implementation details</li> </ol> <p>You seem to be on the right track, but need a little guidance. I would suggest reading material that <a href="http://martinfowler.com/" rel="nofollow noreferrer">Martin Fowler</a>, and <a href="http://www.objectmentor.com/omTeam/martin_r.html" rel="nofollow noreferrer">Bob Martin</a> have to get up to speed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload