rhino-mocks

RhinoMocks: How do you properly mock an IEnumerable<T>?

I just keep stumbling through mocking... The latest disaster was not grokking that I need to actually push results inside a mock object of IEnumerable... Here's a sample (demonstration only of IEnumerable, not actually good Interaction-based testing!): using System; using System.Collections.Generic; using Rhino.Mocks; using MbUnit.Fra...

Mocking Asp.net-mvc Controller Context

So the controller context depends on some asp.net internals. What are some ways to cleanly mock these up for unit tests? Seems like its very easy to clog up tests with tons of setup when I only need, for example, Request.HttpMethod to return "GET". I've seen some examples/helpers out on the nets, but some are dated. Figured this would b...

Rhino Mocks - How can I test that at least one of a group of methods is called?

Say I have an interface IFoo which I am mocking. There are 3 methods on this interface. I need to test that the system under test calls at least one of the three methods. I don't care how many times, or with what arguments it does call, but the case where it ignores all the methods and does not touch the IFoo mock is the failure case. I...

What is the best style/syntax to use with Rhino Mocks?

Multiple approaches exist to write your unit tests when using Rhino Mocks: The Standard Syntax Record/Replay Syntax The Fluent Syntax What is the ideal and most frictionless way? ...

Rhino Mocks: How can I mock out a method that transforms its input?

I have a Data Access Object TransactionDao. When you call TransactionDao.Save(transaction) I would like for it to setting a transaction.IsSaved=true flag (this is a simplification the actual thing I'm trying to do is not quite so banal). So when mocking my TransactionDao with RhinoMocks how can I indicate that it should transform its i...

Rhino Mocks: How do I return numbers from a sequence

I have an Enumerable array int meas[] = new double[] {3, 6, 9, 12, 15, 18}; On each successive call to the mock's method that I'm testing I want to return a value from that array. using(_mocks.Record()) { Expect.Call(mocked_class.GetValue()).Return(meas); } using(_mocks.Playback()) { foreach(var i in meas) Assert.AreEqual(i,...

Rhino Mocks: What scope do actions inside Do() execute in?

I notice that if I write Expect.Call(delegate { obj.Method(null); }).IgnoreArguments().Do( new Action(() => {Console.Write("executed");throw new Exception(); })); Provided that the method does run, MbUnit will recieve the "executed" message but will not detect an exception being thrown. Does anyone know why that is so? Is this som...

Rhino Mocks: Is there any way to verify a constraint on an object property's property?

If I have class ObjA { public ObjB B; } class ObjB { public bool Val; } and class ObjectToMock { public DoSomething(ObjA obj){...} } Is there any way to define an expectation that not only will DoSomething get called but that obj.B.Val == true? I have tried Expect.Call(delegate { mockObj.DoSomething(null);}).Constraints(n...

RhinoMocks: Correct way to mock property getter

I'm new to RhinoMocks, and trying to get a grasp on the syntax in addition to what is happening under the hood. 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 ...

Mocking method results

I'm trying to find a way to fake the result of a method called from within another method. I have a "LoadData" method which calls a separate helper to get some data and then it will transform it (I'm interested in testing the transformed result). So I have code like this: public class MyClass(){ public void LoadData(){ SomePrope...

Rhino mocks ordered reply, throw exception problem

I'm trying to implement some retry logic if there is an exception in my code. I've written the code and now I'm trying to get Rhino Mocks to simulate the scenario. The jist of the code is the following: class Program { static void Main(string[] args) { MockRepository repo = new MockRepository(); ...

Rhino Mocks: Re-assign a new result for a method on a stub

I know I can do this: IDateTimeFactory dtf = MockRepository.GenerateStub<IDateTimeFactory>(); dtf.Now = new DateTime(); DoStuff(dtf); // dtf.Now can be called arbitrary number of times, will always return the same value dtf.Now = new DateTime()+new TimeSpan(0,1,0); // 1 minute later DoStuff(dtf); //ditto from above What if instead of...

Best Practices of Test Driven Development Using C# and RhinoMocks

In order to help my team write testable code, I came up with this simple list of best practices for making our C# code base more testable. (Some of the points refer to limitations of Rhino Mocks, a mocking framework for C#, but the rules may apply more generally as well.) Does anyone have any best practices that they follow? To maximiz...

Rhino Mocks, MbUnit: Best way to check if object has raised an event

I have an object that I'm testing that raises an event. What is the best way of using Rhino Mocks to check that it was raised? Best I could come up with (I am certain it gets better than this): public void MyCallback(object sender, EventArgs e) { _flag = true;} [Test] public void DoSomethingRaisesEvent() { _flag = false; using(...

Rhino Mocks: How to mock ADO.NET's DataRow?

ADO.NET has the notorious DataRow class which you cannot instantiate using new. This is a problem now that I find a need to mock it using Rhino Mocks. Does anyone have any ideas how I could get around this problem? ...

How to set expectations for calls which will be made on another thread in Rhino Mocks

I have a class which is designed to spin up a background thread, from which calls will be made into a manager. This manager will be mocked for the purposes of unit test. The relevant fragment of code is: MockRepository mocks = new MockRepository(); ICacheManager manager = mocks.CreateMock<ICacheManager>(); Expect.On(manager).Call(mana...

Rhino Mocks: good tutorials

I'm looking for a good tutorial on mock objects in general and Rhino Mocks in particular. I'd like to recommend it to co-workers to help bring them up to speed. One co-worker is an experienced developer with some experience writing unit tests, while the other is less experienced. ...

Quick Rhinomocks Help

Can someone take a look at this code and tell me if there's any obvious reason it shouldn't be working? When service.getResponse is called within my code the mocking framework only returns null, not the object I specified. [Test] public void Get_All_Milestones() { var mockRepo = new MockRepository(); var serv...

Can Rhino Mocks Write My Expect Statements For Me?

I have a set of Visual Studio Team System unit (integration really) tests that talk to a remote database. The tests are getting too slow and unwieldy. I'd like to replace the entire set of tests with mocked out versions. The problem is it's painful to write all the expect statements that mimic what an entire database does. Does any...

Is it possible to throw a MessageQueueException?

I am using a mock object in RhinoMocks to represent a class that makes calls to MessageQueue.GetPublicQueues. I want to simulate the exception thrown when message queueing is operating in workgroup mode, which is a MessageQueueException, to ensure that I am catching the exception correctly The MessageQueueException has no public constru...