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...
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...
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...
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?
...
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...
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,...
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...
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...
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 ...
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...
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();
...
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...
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...
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(...
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?
...
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...
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.
...
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...
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...
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...