rhino-mocks

Rhino Mock Entity Framework using UnitofWork Pattern not working

孤人 提交于 2019-12-04 07:29:32
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 == CPUID) > 0) { // do something } return 0; } } Here is my UnitOfWork interface: public interface

How to assert that an action was called

冷暖自知 提交于 2019-12-04 04:38:25
问题 I need to asset an action called by a mock component. public interface IDispatcher { void Invoke(Action action); } public interface IDialogService { void Prompt(string message); } public class MyClass { private readonly IDispatcher dispatcher; private readonly IDialogservice dialogService; public MyClass(IDispatcher dispatcher, IDialogService dialogService) { this.dispatcher = dispatcher; this.dialogService = dialogService; } public void PromptOnUiThread(string message) { dispatcher.Invoke(()

Rhino Mocks Partial Mock

主宰稳场 提交于 2019-12-04 04:23:42
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 without re-factoring the class? I have tried all the different mock types (Strick,Stub,Dynamic,Partial

How to mock IDbSet with Rhino Mocks

允我心安 提交于 2019-12-04 04:05:52
问题 I can't get this working at all. I've got this code in my test: MockRepository repository = new MockRepository(); IDbSet<SystemUser> userSet = repository.StrictMock<IDbSet<SystemUser>>(); Expect.Call(userSet.Any(u => u.Id == "UserName")).Return(true); // More code follows But it bombs out on the StrictMock line with the error: System.TypeLoadException: Method 'Create' on type 'IDbSet`1Proxy1862178487664986a7bd03ad3b5c6f2c' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture

Mocking method results

拟墨画扇 提交于 2019-12-03 17:41:38
问题 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(){ SomeProperty = Helper.GetSomeData(); } public object SomeProperty {get;set;} } I want to have a known result from the Helper.GetSomeData() method. Can I use a mocking framework

Mocking GetEnumerator() method of an IEnumerable<T> types

…衆ロ難τιáo~ 提交于 2019-12-03 16:37:08
问题 The following test case fails in rhino mocks: [TestFixture] public class EnumeratorTest { [Test] public void Should_be_able_to_use_enumerator_more_than_once() { var numbers = MockRepository.GenerateStub<INumbers>(); numbers.Stub(x => x.GetEnumerator()).Return(new List<int> { 1, 2, 3 }.GetEnumerator()); var sut = new ObjectThatUsesEnumerator(); var correctResult = sut.DoSomethingOverEnumerator2Times (numbers); Assert.IsTrue(correctResult); } } public class ObjectThatUsesEnumerator { public

RhinoMocks - mocking a method whose return value changes (even when passed the same parameter) with multiple calls

妖精的绣舞 提交于 2019-12-03 15:00:50
问题 I'm looking to find out how I can mock a method that returns a different value the second time it is called to the first time. For example, something like this: public interface IApplicationLifetime { int SecondsSinceStarted {get;} } [Test] public void Expected_mock_behaviour() { IApplicationLifetime mock = MockRepository.GenerateMock<IApplicationLifetime>(); mock.Expect(m=>m.SecondsSinceStarted).Return(1).Repeat.Once(); mock.Expect(m=>m.SecondsSinceStarted).Return(2).Repeat.Once(); Assert

How can I test an event of a MVC controller

我只是一个虾纸丫 提交于 2019-12-03 14:38:03
I want to test the OnException , OnActionExecuted event of an MVC controller. If I use mock like this: var httpContext = MockRepository.GenerateMock<HttpContextBase>(); var request = MockRepository.GenerateMock<HttpRequestBase>(); httpContext.Expect(c => c.Request).Return(request).Repeat.AtLeastOnce(); request.Expect(r => r.IsAuthenticated ).Return(true).Repeat.AtLeastOnce(); var controller = new MyController() ; controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller); var result = controller.Execute() as ViewResult; …the action method is executing, but

Counting method calls in Rhino Mocks

浪尽此生 提交于 2019-12-03 13:02:21
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? Onots 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 = MockRepository.GenerateMock<IExample>(); mock.Expect(example => example.ExampleMethod()).Repeat.Times

How do I mock IQueryable<T>

别等时光非礼了梦想. 提交于 2019-12-03 11:31:51
问题 I am creating a repository that exposes IQueryable. What is the best way to mock this out for my unit testing? Since I am using RhinoMocks for the rest of my mock objects, I tried to do the following: IQueryable<MyObject> QueryObject = MockRepository.GenerateStub<IQueryable<MyObject>>(); This doesn't work though so I tried doing this: IQueryable<MyObject> QueryObject = (new List<MyObject> { new MyObject() }).AsQueryable(); Is there a better way to do this, or have any other mocking frameworks