rhino-mocks

What is the purpose of unit testing an interface repository

拜拜、爱过 提交于 2019-12-05 08:07:47
I am unit testing an ICustomerRepository interface used for retrieving objects of type Customer . As a unit test what value am I gaining by testing the ICustomerRepository in this manner? Under what conditions would the below test fail? For tests of this nature is it advisable to do tests that I know should fail? i.e. look for id 4 when I know I've only placed 5 in the repository I am probably missing something obvious but it seems the integration tests of the class that implements ICustomerRepository will be of more value. [TestClass] public class CustomerTests : TestClassBase { private

Rhino Mocks: Can I use Stub() when one of my parameters is Expression<Func<T1, T2>>?

孤街醉人 提交于 2019-12-05 07:36:46
I have a method on an interface that looks like this and I want to stub it with Rhino Mocks: TValue GetPropertyOfExistingObject<TValue>(long id, Expression<Func<T, TValue>> propertyExpression); My code that does the stubbing looks like this: var service = MockRepository.GenerateStub<IQuoteService>(); service.Stub(s => s.GetPropertyOfExistingObject(1, q => q.QuoteNumber)).Return(1234); Notice that one of the parameters in that method is an Expression<Func<T1, T2>> , and this stub is not returning the specified value. I know I can do this by using WhenCalled() but I was wondering if Stub()

Rhino Mocks: How to stub a generic method to catch an anonymous type?

醉酒当歌 提交于 2019-12-05 00:45:52
We need to stub a generic method which will be called using an anonymous type as the type parameter. Consider: interface IProgressReporter { T Report<T>(T progressUpdater); } // Unit test arrange: Func<object, object> returnArg = (x => x); // we wish to return the argument _reporter.Stub(x => x.Report<object>(null).IgnoreArguments().Do(returnArg); This would work if the actual call to .Report<T>() in the method under test was done with object as the type parameter, but in actuality, the method is called with T being an anonymous type. This type is not available outside of the method under test

Counting method calls in Rhino Mocks

独自空忆成欢 提交于 2019-12-04 20:43:38
问题 So: I'd like to count method calls in Rhino Mocks with something more specific than Any(), Once() or AtLeastOnce(). Is there any mechanism for doing this? 回答1: The trick is to use Repeat.Times(n), where n is the number of times. Suprisingly the below test will pass, even if the method is called more often than expected: [Test] public void expect_repeat_n_times_does_not_work_when_actual_greater_than_expected() { const Int32 ActualTimesToCall = 6; const Int32 ExpectedTimesToCall = 4; var mock =

What is ReplayAll() and VerifyAll() in RhinoMocks

↘锁芯ラ 提交于 2019-12-04 20:35:41
问题 [Test] public void MockAGenericInterface() { MockRepository mocks = new MockRepository(); IList<int> list = mocks.Create Mock<IList<int>>(); Assert.IsNotNull(list); Expect.Call(list.Count).Return(5); mocks.ReplayAll(); Assert.AreEqual(5, list.Count); mocks.VerifyAll(); } What is the purpose of ReplayAll() and VerifyAll() in this code? 回答1: The code snippet demonstrates the Record/Replay/Verify syntax of Rhino.Mocks. You first record the expectations for a mock (using Expect.Call() , then you

Approach to unit testing interaction with a base class

独自空忆成欢 提交于 2019-12-04 13:50:32
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 method is Protected , it's Protected because that's the appropriate design for it (I actually have yet to

Unit Testing custom ConfigurationElement & ConfigurationElementCollection

时光毁灭记忆、已成空白 提交于 2019-12-04 11:44:28
问题 I have created a custom ConfigurationElement and ConfigurationSection to make it easier to set up a host of application parameters at startup. However, I'd really like to unit test this logic. ServiceConnection public class ServiceConnection : ConfigurationElement { [ConfigurationProperty("locationNumber", IsRequired = true)] public string LocationNumber { get { return (string) base["locationNumber"]; } set { base["locationNumber"] = value; } } [ConfigurationProperty("hostName", IsRequired =

How can I mock Server.HtmlEncode

孤街醉人 提交于 2019-12-04 10:00:57
I am trying the following, but I am getting : Object reference not set to an instance of an object. HttpContextBase mockContext = MockRepository.GenerateMock<HttpContextBase>(); mockContext.Expect(c => c.Server.HtmlEncode("")).IgnoreArguments().Return(""); mockContext.Expect(c => c.Server.HtmlDecode("")).Return(""); controller.ControllerContext = new ControllerContext(mockContext, new RouteData(), controller); Matin, Thanks. That was enough to point me in the right direction provided here: var mockContext = MockRepository.GenerateMock<HttpContextBase>(); var mockServer = MockRepository

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

跟風遠走 提交于 2019-12-04 09:07:20
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 reference to Rhino.Mocks.dll and attempted to re-add it. Then I get a dialog that reads: 'Rhino.Mocks

Rhino Mocks & Compact Framework

若如初见. 提交于 2019-12-04 08:51:54
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 SmartDeviceTest folder that gets created) but it still can't seem to find it. Has anyone used rhino