rhino-mocks

Approach to unit testing interaction with a base class

人走茶凉 提交于 2019-12-06 07:04:27
问题 There seems to be three general patterns for testing the way a derived class interacts with it's base 1 . I don't really like any of them, for the reasons I've listed below. Has anyone had long term success with one of these (or another) methods for testing base class interaction? Change the access modifiers to allow Friend ( internal in C#) access and set InternalsVisibleTo to include the mocking framework / unit test assembly Changing the SUT to allow for testing is a test smell . If the

Unable to add Rhino Mocks 3.5 to a .NET 2.0 project in Visual Studio 2010

梦想的初衷 提交于 2019-12-06 05:33:11
问题 We're upgrading from Dev Studio 2005 to Dev Studio 2010. I opened my 2005 solution in Visual Studio 2010 and went through the conversion process keeping all projects targeted at .NET 2.0. When I try to build the project, my references to Rhino.Mocks.dll are failing to be used. I see errors like this: DalDiscoveryTest.cs(7,7): error CS0246: The type or namespace name 'Rhino' could not be found (are you missing a using directive or an assembly reference?) I went into my project and removed the

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

◇◆丶佛笑我妖孽 提交于 2019-12-06 05:24:06
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 IDateTimeFactory.Now being a property it is a method IDateTimeFactory.GetNow() , how do I do the same thing? As per Judah's suggestion below I have rewritten my SetDateTime helper method as follows: private void SetDateTime(DateTime dt) { Expect.Call(_now_factory

Rhino Mocks & Compact Framework

拜拜、爱过 提交于 2019-12-06 03:10:48
问题 I've been experimenting with Rhino Mocks for unit testing my .Net Compact Framework application, and have hit a problem. I can get a basic unit test using Rhino Mocks built, but every time I run the test that has the Rhino Mocks code in it, the test fails because it can't find the Rhino Mocks assembly. System.TypeLoadException: Could not load type 'Rhino.Mocks.MockRepository' from assembly 'Rhino.Mocks... I've copied the rhino mocks dll to various places on the device (my app folder, and the

Rhino Mock Entity Framework using UnitofWork Pattern not working

我怕爱的太早我们不能终老 提交于 2019-12-06 03:07:14
问题 This is my first attempt at something like this, so hopefully this is simple. I have created a WCF service which uses Entity Framework to access the database. I have implemented a UnitOfWork interface so my service can use EF while still being testable. Here is my service: public class ProjectService : IProjectService { private IUnitOfWork db = null; public ProjectService(IUnitOfWork unitofwork) { db = unitofwork; } public int RegisterSite(int CPUID) { if (db.Servers.Count(x => x.CPUID ==

Mocking lambda in rhino mocks

柔情痞子 提交于 2019-12-05 19:25:56
I am trying to use Rhino Mocks to mock the following lambda, but keep hitting a brick wall var result = rep.Find<T>(x => (x as IEntity).ID == (entity as IEntity).ID).FirstOrDefault(); Any ideas? In a unit test, you have the system under test (SUT) and its collaborators. The goal of mocking is to replace the collaborators by something that you have full control over. That way you can set up different test cases and you can focus on testing just the behavior of the system under test, and nothing else. In this case, I assume the rep object is the SUT. The lambda that you pass to the SUT's Find

Mocking a private field

倾然丶 夕夏残阳落幕 提交于 2019-12-05 19:03:38
I know a similar question has been asked but I have not found a clear solution. I'm trying to mock a private field from a large class. The private field gets instantiated in some earlier method and I'm trying to unit test a latter method which references the field. So I have an earlier method in my class: public bool validateAll(ref DataEntry[] oEntries, string sMediaPlanId, ITemplateGenerator oTempGen) { ... // private field that I am trying to mock this._sMediaPlanObjective = (MPWrapper.Instance).getMediaPlanObjective(sMediaPlanId); ... } And I'm trying to Unit test a method that references

Multiple calls to a Rhino mocked method return different results

我的未来我决定 提交于 2019-12-05 12:32:36
问题 If I want to mock a class that returns a string that is used to determine whether while loop should continue (imagine read while string != null), how can I set the expectation. I have tried the following: provider.Reader.Expect(r => r.ReadLine()).Return("1,10,20"); provider.Reader.Expect(r => r.ReadLine()).Return(null); but when it is called twice in the same method, it returns the first string on both occasions, whereas I want it to return the second value (null) if called a second time. 回答1

Unit Testing error “Object reference not set to an instance of an object.”

爷,独闯天下 提交于 2019-12-05 11:09:33
In my controller I want to test if the controller is calling the repository method. Here is the method in controller [HttpGet] public ActionResult GetModulePropertyName(string moduleTypeValue) { var temp = _modulerepository.GetModuleKindPropertyNames(moduleTypeValue); IList<Property> model = temp .Select(item => new Property {Name = item}) .ToList(); return PartialView("GetModulePropertyName",model); } And here is the test method [TestMethod] public void GetModulePropertyName_Action_Calls_GetModuleKindPropertyNames() { _mockRepository.Stub(x => x.GetModuleKindPropertyNames(Arg<string>.Is

Rhino - Mocking classes and not overriding virtual methods

北慕城南 提交于 2019-12-05 10:00:23
If I'm mocking a class, like below, is there any way I can get the mock to not override a virtual method? I know I can simply remove the virtual modifier, but I actually want to stub out behavior for this method later. In other words, how can I get this test to pass, other than removing the virtual modifier: namespace Sandbox { public class classToMock { public int IntProperty { get; set; } public virtual void DoIt() { IntProperty = 1; } } public class Foo { static void Main(string[] args) { classToMock c = MockRepository.GenerateMock<classToMock>(); c.DoIt(); Assert.AreEqual(1, c.IntProperty)