rhino-mocks

Mocking WebSecurity provider

╄→гoц情女王★ 提交于 2019-12-10 18:24:44
问题 I'm trying to create some simple unit tests for my controllers and I've come across an issue. I'm using the new membership provider in MVC 4 and getting the WebSecurity.CurrentUserId and storing that value in the database. When I run my unit tests against this it's failing and I think I have track this back to the fact that the WebSecurity isn't being mocked at all. Here's my code if it helps at all, The Controller [HttpPost] public ActionResult Create(CreateOrganisationViewModel viewModel) {

Rhino Mocks - Testing Repository layer returns “object reference not set to instance” error

眉间皱痕 提交于 2019-12-10 18:13:08
问题 Its prudent that I firstly say I'm new to both Rhino Mocks and to mocking more generally. With that in mind i'm attempting to Unit test my Linq to SQL repository layer to ensure that the correct methods on the datacontext are being hit, and that the LINQ to SQL is filtering correctly. ~EDITED for clarity~ The method in question - 'GetRecordWhere' - is defined in the Repository class. It calls the method - 'GetTable' - on the DataContextWrapper which is my custom wrapper around the Linq to SQL

rhino mocks: how to build a fake socket?

依然范特西╮ 提交于 2019-12-10 18:05:30
问题 I tried to build a fake socket for testing using the following code: var socket = MockRepository.GenerateStub<Socket>( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP ); socket.Stub(v => v.RemoteEndPoint).PropertyBehavior().Return( new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345) ); However, the attempt to create the stub for the read only-property gives me the following exception: Invalid call, the last call has been used or no call has been made (make sure that you are

Stubbing a static extension method seems to work in Rhino Mocks unless I cast the return variable. Why?

霸气de小男生 提交于 2019-12-10 17:37:53
问题 I am able to stub a static extension method using Rhino Mocks but if I cast the return value to another type, I get an error. Why? using Rhino.Mocks; public interface INumberGenerator { double GetDouble(); } static class NumberGeneratorExtensionMethods { public static double GetTheDouble(this INumberGenerator input) { return input.GetDouble(); } public static decimal GetTheDoubleCastToDecimal(this INumberGenerator input) { return (decimal) input.GetDouble(); } } class MockExample { public

Mock File IO static class in c#

一笑奈何 提交于 2019-12-10 13:29:42
问题 I am new to Unit Testing, and I need to mock the File static class in System.IO namespace. I am using Rhinomock, what is the best way to accomplish this, Lets say I need to mock the File.Exists,File.Delete ... 回答1: You can't mock static methods with Rhino mock. See this question for more info. You could create a facade class to wrap the file system calls you will use and then create a mock version of that. 回答2: You should create a wrapper service called IFileService, then you can create a

How to mock protected virtual members with Rhino.Mocks?

六月ゝ 毕业季﹏ 提交于 2019-12-10 13:09:22
问题 Moq allows developers to mock protected members. I was looking for the same functionality in Rhino.Mocks but fail to find it. Here's an example from Moq Quick Start page how to mock protected method. // at the top of the test fixture using Moq.Protected() // in the test var mock = new Mock<CommandBase>(); mock.Protected() .Setup<int>("Execute") .Returns(5); // if you need argument matching, you MUST use ItExpr rather than It // planning on improving this for vNext mock.Protected() .Setup

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

拟墨画扇 提交于 2019-12-10 11:22:50
问题 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

Extension methods in Mono 2.4 and RhinoMocks 3.5

一笑奈何 提交于 2019-12-10 11:04:24
问题 I am playing around with MonoDevelop 2.0 and Mono 2.4 in Ubuntu. I have run into problems with extension methods not being available (eg mockView.Stub(...)) in RhinoMocks 3.5 for AAA style tests. I downloaded the RhinoMocks dll from Ayende's site rather than compiled from source. My project in MonoDevelop is setup to target framework 3.5 Using the RhinoMocks c#2.0 syntax with static methods on the RhinoMocksExtensions class works. (e.g RhinoMocksExtensions.Stub(authSvc, delegate(IAuthService

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

巧了我就是萌 提交于 2019-12-10 01:48:16
问题 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

Rhino Mocks Partial Mock

耗尽温柔 提交于 2019-12-09 16:43:12
问题 I am trying to test the logic from some existing classes. It is not possible to re-factor the classes at present as they are very complex and in production. What I want to do is create a mock object and test a method that internally calls another method that is very hard to mock. So I want to just set a behaviour for the secondary method call. But when I setup the behaviour for the method, the code of the method is invoked and fails. Am I missing something or is this just not possible to test