Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are mocking a concrete object here. Moq works best when used with an interface. Try to <a href="http://www.google.co.uk/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;ved=0CDQQFjAA&amp;url=http://jmock.org/oopsla2004.pdf&amp;ei=mQUpUb_1IbKN0wXqloHIBA&amp;usg=AFQjCNG1hoMD7NunoONsaOmVDgaQYkNmFA&amp;sig2=7bAySxnUn5GMNBCs12mjBg&amp;bvm=bv.42768644,d.d2k" rel="nofollow">mock roles, not objects</a> (pdf). For example, introduce an interface such as <code>IObjectRegistry</code>. At present your code knows it is dealing with an ObjectRegistry, when ideally you should be able to switch your implementation out whenever you wish. Providing you have an abstraction in place. It would be ideal both for testability and programming best practices to "<em>program to an interface</em>" instead. Note interface here does not necessarily mean an interface in terms of C#/Java etc.</p> <pre><code>var registry = new Mock&lt;IObjectRegistry&gt;(); public class ObjectRegistry : IObjectRegistry { // Snip } </code></pre> <p>Then use this. As this is an interface, you can freely change the concrete (real/production) class and its constructor as you wish. Your tests and the rest of the system should be unaware. If the registry had one method on the interface such as <code>GetObject</code> your system should simply rely on this abstraction, not the fact it is working with an <code>ObjectRegistry</code> instance.</p> <p>Currently your code is actually creating a real instance of an <code>ObjectRegistry</code>, then stubbing/mocking the methods in question. This is not ideal in most cases.</p>
 

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