rhino-mocks

Why can't I change the return value in a Rhino Mocks stub object?

限于喜欢 提交于 2020-01-14 10:44:12
问题 Forgive me if this is a stupid question, but I'm fairly new at mocking, and am trying to get my head around it. I have some unit tests (using the built-in Visual Studio 2010 Professional testing features), which use a stub that is needed by a method. I created a stub, and set the default return values for a couple of properties and a method, and all works well. I have a static class that sets up the stub, and this is used in the TestInitialize method to set up the stub... public static

Mocking iterative behaviour

让人想犯罪 __ 提交于 2020-01-11 14:00:31
问题 I have an interface with iterative behaviour, and I am having trouble Mocking that in Rhinomocks. The example interface and class is a very simple version of my problem. Every time LineReader.Read() is called, the LineReader.CurrentLine() should return a different value -- the next line. This behaviour I haven't been able to reproduce in a mock so far. Thus, it has become a small hobby project of mine which I return to from time to time. I hope you can help me a step further. internal class

How to mock a property setter on a PartialMock using Rhino Mocks

萝らか妹 提交于 2020-01-06 18:08:51
问题 I'd like to prevent the real setter code being invoked on a property on a partial class. What is the syntax for this? My current code to stub out the getter (I'd like to also stub out the setter): var user = MockRepository.GeneratePartialMock<User>(ctor params...); user.MyProperty = "blah"; Something like this? user.Stub(u => u.MyProperty).Do(null); 回答1: Here's a 3.5 sample that does what you need (I think your syntax above is 3.1 or 3.2). First, I have a delegate for the property setter call

How to mock a property setter on a PartialMock using Rhino Mocks

拟墨画扇 提交于 2020-01-06 18:08:31
问题 I'd like to prevent the real setter code being invoked on a property on a partial class. What is the syntax for this? My current code to stub out the getter (I'd like to also stub out the setter): var user = MockRepository.GeneratePartialMock<User>(ctor params...); user.MyProperty = "blah"; Something like this? user.Stub(u => u.MyProperty).Do(null); 回答1: Here's a 3.5 sample that does what you need (I think your syntax above is 3.1 or 3.2). First, I have a delegate for the property setter call

Mock RouteData in RhinoMocks-MVC3

て烟熏妆下的殇ゞ 提交于 2020-01-06 11:47:36
问题 How to Mock RouteData in RhinoMock? I have to Mock this. (RouteData.Values["id"].Tostring()!=null) Thank you 回答1: Given that RouteData is of type IRouteData you can mock it like this: [Test] public void TestMockingConcreteClass() { MockRepository mockRepository = new MockRepository(); RouteData routeData = mockRepository.Stub<RouteData>(); routeData.Stub(r => r.Values["id"]).Return("XXX"); mockRepository.ReplayAll(); Assert.That(routeData.Values["id"].ToString(), Is.EqualTo("XXX")); } 回答2:

How to assert if a method was called within another method in RhinoMocks?

ⅰ亾dé卋堺 提交于 2020-01-06 08:37:32
问题 I have a class that has two methods. One method needs to call the other method and in my test I want to assert that it was called. public class Tasks : ITasks { public void MethodOne() { MethodTwo(1); } public int MethodTwo(int i) { return i + 1; } } I want to mock Tasks and do something like tasks.AssertWasCalled(x => x.MethodTwo(1)) . Must MethodTwo be virtual? 回答1: The concept you're looking for is partial mocks (this shows old syntax, but I don't remember the new one off the top of my

Rhino Mock throws an exception when raising an event

混江龙づ霸主 提交于 2020-01-06 02:51:19
问题 Is it possible to simulate an event using Rhino Mocks framework without getting the exception below? A first chance exception of type 'System.NotSupportedException' occurred in Rhino.Mocks.dll - Can't create mocks of sealed classes public interface IWithEvent { event EventHandler RaiseMeWithoutExceptionPlease; } [TestClass] public class MockedEvents { [TestMethod] public void EventsTest() { IWithEvent withEvent = MockRepository.GenerateStub<IWithEvent>(); for (int i = 0; i < 500; i++) { //

Trying to stub Server.MapPath with MvcContrib Test helpers and Rhino Mocks 3.5

和自甴很熟 提交于 2020-01-05 08:27:27
问题 I'm using MvcContrib's test helpers and Rhino Mocks 3.5 to test an ASP.NET MVC action method. I build my fake controller like so: var _builder = new TestControllerBuilder(); _builder.InitializeController(_controller); So I get a fake controller that contains fake HTTP Server etc. I'm then trying to stub the Server.MapPath method like so controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/APP_DATA/Files/"); but in my method under test the call to Server.MapPath("/APP_DATA

How Structure Map Automocker Inject works?

☆樱花仙子☆ 提交于 2020-01-04 12:47:41
问题 I have constructor containing IEnumerable parameter. When I try to Inject concrete object to automocker it is not used. When I use wrapper class containing IEnumerable property all works as expected. How can I test TestClass1? IEnumerable parameter public class TestClass1 { public TestClass1(IEnumerable<IItem> items) { Items = items; } public IEnumerable<IItem> Items { get; private set; } } [TestMethod] public void TestClass1Constructor() { RhinoAutoMocker<TestClass1> autoMocker = new

How to mock HttpContext.Current.GetOwinContext() with Nunit and RhinoMocks?

大城市里の小女人 提交于 2020-01-04 04:06:34
问题 Compared to my last question on Mocking the HttpContext, I had to change the method being tested to public override void OnActionExecuting(HttpActionContext actionContext) { HttpContext.Current.GetOwinContext().Set("RequestGUID", NewId.NextGuid()); base.OnActionExecuting(actionContext); } Now I need to figure out how to mock the HttpContext.Current.GetOwinContext() , So I could write a stub for the Set() method, or generally being able to test this particular line. How can I do this? 回答1: I