<p>I'm new to RhinoMocks, and trying to get a grasp on the syntax in addition to what is happening under the hood.</p>

<p>I have a user object, we'll call it User, which has a property called IsAdministrator. The value for IsAdministrator is evaluated via another class that checks the User's security permissions, and returns either true or false based on those permissions. I'm trying to mock this User class, and fake the return value for IsAdministrator in order to isolate some Unit Tests.</p>

<p>This is what I'm doing so far:</p>

<pre><code>public void CreateSomethingIfUserHasAdminPermissions()
{
    User user = _mocks.StrictMock&lt;User&gt;();
    SetupResult.For(user.IsAdministrator).Return(true);

    // do something with my User object
} 
</code></pre>

<p>Now, I'm expecting that Rhino is going to 'fake' the call to the property getter, and just return true to me. Is this incorrect? Currently I'm getting an exception because of dependencies in the IsAdministrator property.</p>

<p>Can someone explain how I can achieve my goal here?</p>

[{'answer_id': 78428, 'author': 'Aaron Jensen', 'author_id': 11229, 'author_profile': 'https://Stackoverflow.com/users/11229', 'pm_score': 1, 'selected': False, 'text': '<p>Make sure IsAdministrator is virtual.</p>\n\n<p>Also, be sure you call _mocks.ReplayAll()</p>\n'}, {'answer_id': 79719, 'author': 'Josh', 'author_id': 11702, 'author_profile': 'https://Stackoverflow.com/users/11702', 'pm_score': 5, 'selected': True, 'text': '<p>One quick note before I jump into this. Typically you want to avoid the use of a "Strict" mock because it makes for a brittle test. A strict mock will throw an exception if anything occurs that you do not explicitly tell Rhino will happen. Also I think you may be misunderstanding exactly what Rhino is doing when you make a call to create a mock. Think of it as a custom Object that has either been derived from, or implements the System.Type you defined. If you did it yourself it would look like this:</p>\n\n<pre><code>public class FakeUserType: User\n{\n    //overriding code here\n}\n</code></pre>\n\n<p>Since IsAdministrator is probably just a public property on the User type you can\'t override it in the inheriting type.</p>\n\n<p>As far as your question is concerned there are multiple ways you could handle this. You could implement IsAdministrator as a virtual property on your user class as <a href="https://stackoverflow.com/questions/78389/rhinomocks-correct-way-to-mock-property-getter#78428">aaronjensen</a> mentioned as follows:</p>\n\n<pre><code>public class User\n{\n    public virtual Boolean IsAdministrator { get; set; }\n}\n</code></pre>\n\n<p>This is an ok approach, but only if you plan on inheriting from your User class. Also if you wan\'t to fake other members on this class they would also have to be virtual, which is probably not the desired behavior.</p>\n\n<p>Another way to accomplish this is through the use of interfaces. If it is truly the User class you are wanting to Mock then I would extract an interface from it. Your above example would look something like this:</p>\n\n<pre><code>public interface IUser\n{\n    Boolean IsAdministrator { get; }\n}\n\npublic class User : IUser\n{\n    private UserSecurity _userSecurity = new UserSecurity();\n\n    public Boolean IsAdministrator\n    {\n        get { return _userSecurity.HasAccess("AdminPermissions"); }\n    }\n}\n\npublic void CreateSomethingIfUserHasAdminPermissions()\n{\n    IUser user = _mocks.StrictMock&lt;IUser&gt;();\n    SetupResult.For(user.IsAdministrator).Return(true);\n\n    // do something with my User object\n}\n</code></pre>\n\n<p>You can get fancier if you want by using <a href="http://martinfowler.com/articles/injection.html" rel="nofollow noreferrer">dependency injection and IOC</a> but the basic principle is the same across the board. Typically you want your classes to depend on interfaces rather than concrete implementations anyway.</p>\n\n<p>I hope this helps. I have been using RhinoMocks for a long time on a major project now so don\'t hesitate to ask me questions about TDD and mocking.</p>\n'}, {'answer_id': 2598593, 'author': 'Pavlo Neiman', 'author_id': 164001, 'author_profile': 'https://Stackoverflow.com/users/164001', 'pm_score': 0, 'selected': False, 'text': '<p>_mocks.ReplayAll() will do nothing. It is just because you use SetupResult.For() that does not count. Use Expect.Call() to be sure that your code do everything correct.</p>\n'}]
['https://Stackoverflow.com/questions/78389', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/769/']