rhino-mocks

Stubbing a read only property with Rhino Mocks

与世无争的帅哥 提交于 2019-12-21 07:06:06
问题 I have a class with a private set property that I want to stub out with rhino mocks. When I try to do this, though, it gives me a compile time error saying I can't set a read only property. I'm new to using Rhino Mocks so I must be missing something here... public Interface IFoo { int Quantity { get; } } [TestMethod] public void SomeTest() { IFoo foo = MockRepository.GenerateStub<IFoo>(); foo.Quantity = 5; //Asserts and such } 回答1: Use: foo.Stub (f => f.Quantity).Return (5); See http://ayende

How can I test an event of a MVC controller

陌路散爱 提交于 2019-12-21 04:42:40
问题 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(

RhinoMocks: Correct way to mock property getter

浪子不回头ぞ 提交于 2019-12-21 03:42:47
问题 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 based on those permissions. I'm trying to mock this User class, and fake the return value for IsAdministrator in order to isolate some Unit Tests. This is what

Asserting that a method is called exactly one time

拈花ヽ惹草 提交于 2019-12-20 11:12:54
问题 I want to assert that a method is called exactly one time. I'm using RhinoMocks 3.5. Here's what I thought would work: [Test] public void just_once() { var key = "id_of_something"; var source = MockRepository.GenerateStub<ISomeDataSource>(); source.Expect(x => x.GetSomethingThatTakesALotOfResources(key)) .Return(new Something()) .Repeat.Once(); var client = new Client(soure); // the first call I expect the client to use the source client.GetMeMyThing(key); // the second call the result should

How To Write Unit Test For Method Returning JsonResult With RenderPartialViewToString?

巧了我就是萌 提交于 2019-12-20 10:57:23
问题 If you look at the example at this link: http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/ How would one write a unit test for the JsonAdd method? I have a similar situation in my own code, but the RenderPartialViewToString errors when calling: ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView I've tried different ways of trying to stub that call to no avail. Any help appreciated. Thanks. 回答1: Since ViewEninges is a static class, you can't mock it

unit test with lambda fail using rhino mock

邮差的信 提交于 2019-12-20 07:39:18
问题 If I have this test Expect.Call(_session.Single<Admin>(x => x.Email == userModel.Email)).Repeat.Once().Return(null); Telling me Rhino.Mocks.Exceptions.ExpectationViolationException : ISession.Single(x => (x.Email == value(Enquete.Test.Controllers.MemberControllerTest+<>c__DisplayClassb).userModel.Email)); Expected #1, Actual #0. It fails but if I add .IgnoreArguments() it works. Is it possible to test using lambda? If I debug I can see that my Email is the same. Here's the full test : [Test]

unit test with lambda fail using rhino mock

坚强是说给别人听的谎言 提交于 2019-12-20 07:39:14
问题 If I have this test Expect.Call(_session.Single<Admin>(x => x.Email == userModel.Email)).Repeat.Once().Return(null); Telling me Rhino.Mocks.Exceptions.ExpectationViolationException : ISession.Single(x => (x.Email == value(Enquete.Test.Controllers.MemberControllerTest+<>c__DisplayClassb).userModel.Email)); Expected #1, Actual #0. It fails but if I add .IgnoreArguments() it works. Is it possible to test using lambda? If I debug I can see that my Email is the same. Here's the full test : [Test]

Rhinomocks - Mocking delegates

我只是一个虾纸丫 提交于 2019-12-20 02:36:17
问题 public interface IServiceInvoker { R InvokeService<T, R>(Func<T, R> invokeHandler) where T : class; } public class MediaController : Controller { private IServiceInvoker _serviceInvoker; public MediaController(IServiceInvoker serviceInvoker) { _serviceInvoker = serviceInvoker; } public JsonResult GetAllMedia() { var media = _serviceInvoker.InvokeService<IMediaService, List<MediaBase>>(proxy => proxy.GetAllMediaInJson()); JsonResult jsonResult = new JsonResult(); jsonResult.Data = media;

Unit Testing and Mocking using RhinoMocks

£可爱£侵袭症+ 提交于 2019-12-19 11:18:04
问题 I am trying to setup tests for my new projects and come across some difficulties. I am using NUnit and Rhino Mocks. The Code that I am trying to test is this, public DocumentDto SaveDocument(DocumentDto documentDto) { Document document = null; using (_documentRepository.DbContext.BeginTransaction()) { try { if (documentDto.IsDirty) { if (documentDto.Id == 0) { document = CreateNewDocument(documentDto); } else if (documentDto.Id > 0) { document = ChangeExistingDocument(documentDto); } document

RhinoMocks - Not specifying all parameters in AssertWasCalled

℡╲_俬逩灬. 提交于 2019-12-18 18:34:23
问题 I am using RhinoMocks. Now I want to assert that some function was called, but I only care about one of the arguments. Can I do a AssertWasCalled where I only specify one argument? In the following example I'd like the ignore what was sent to the second argument of SomeOtherFunction(). I.e. I want to check that SomeOtherFunction was called with first parameter 123 and I don't care what the second parameter was. [Test] public void SomeTest() { var myMock = MockRepository.GenerateMock