c#,unit-testing,mocking,tdd,rhino-mocks
The problem was that with AssertWasCalled(m => p.Thing = new ThingClass() you weren't setting expectations or stubs on your strict mock. That's why it crashed when Thing property was set. I don't think you need .Repeat.Once() on your expectation - it's the default option....
Probably you want to use WhenCalled method: class Program { static void Main() { List<IEmailConfiguration> performedCalls = new List<IEmailConfiguration>(); // preparing test instance var mailerMock = MockRepository.GenerateStrictMock<Mailer>(); mailerMock.Expect(x => x.SendMessage(Arg<DummyEmailConfiguration>.Is.Anything)) .WhenCalled(methodInvocation => // that's what you need { var config = methodInvocation.Arguments.OfType<IEmailConfiguration>().Single(); performedCalls.Add(config); }); // testing for...
Stub by definition will not check the expectation, see also this. However, you can verify the expectation simply by calling AssertWasCalled You can modify your code as follows: var autoMocker = new RhinoAutoMocker<Foo>(MockMode.AAA); var barMock = autoMocker.Get<IBar>(); autoMocker.ClassUnderTest.DoSomeThing(); // Bara() should not be called more than four times barMock.AssertWasCalled(bar =>...
c#,.net,async-await,rhino-mocks
An async method runs synchronously until an await is reached. That means if you wait before returning a task in the mocked operation this wait will be synchronous. What you want to do is return a task immediately that completes after some time. You can do that with Task.Delay: numberGenerator.Expect(x...
c#,task-parallel-library,rhino-mocks
The simplest thing is just to return a completed task with the expected result: var responseTask = Task.FromResult(response); I imagine reason this hangs is that the mocked task is never started and hence the given func is not run. You could start it in your test: var responseTask = MockRepository.GenerateMock<Task<HttpResponseMessage>>(taskFunc);...
There are syntax errors in your sample implementation code. Furthermore, in order to set a concrete method of a Mock to return a value, the target method must be marked Virtual or Override. Here is code that should work as desired: public abstract class Base { } public class BaseImplA...
The code above is already a result of some investigation, so it only contains the lines relevant to the problem and its solution. The culprit is the last line of the Arrange part. As it turns out, the Assert line doesn't really check whether the DoSomething method was called on...
c#,delegates,rhino-mocks,stubbing
It seems that I have finally found the answer to my question. It turns out that, quoting section 26.3.1 from this link: Specifically, a delegate type D is compatible with an anonymous method or lambda-expression L provided: If L is a lambda expression that has an implicitly typed parameter list,...
You can't mock methods that are not overridable. When creating mock instance Rhino does the following: It generates dynamic assembly in runtime (using Castle Dynamic Proxy library to do that) In that assembly Rhino creates a new type, deriving from the type you want to mock Members of that new...
c#,unit-testing,nunit,rhino-mocks
Rhino Mock provides Do() handler to access stubbed methods arguments at the time when stubbed method is called. So, it is possible to write a custom handler for Add(MyObj) method which can store the necessary fields of myObj argument passed. And then it is possible to run asserts against stored...
c#,unit-testing,tdd,rhino-mocks,anonymous-types
Finally solved it...the answer was on stackoverflow (where else?) C# ‘dynamic’ cannot access properties from anonymous types declared in another assembly To summarize: Class under test (HttpModule Assembly) context.Response.RedirectToRoute(new { Controller = "C1", Action = "A1" }); Test Class (HttpModuleTest Assembly) var actual = this.ResponseMock .GetArgumentsForCallsMadeOn(r => r.RedirectToRoute(new { Controller...
You are asserting the behaviour of Location and Manufacturing, so these are the objects which should be mocked. Also, when checking that something happens use Expects not Stub. Everything else should be concrete. If you make ResetAllProperties methods virtual then the following works: [TestMethod] public void ResetAllProperties_AssertWasCalled() { var location...
c#,unit-testing,mocking,nunit,rhino-mocks
In your current setup this what you're trying to do is impossible. Few things to fix: start using stable version of Rhino (like 3.6). Better yet, upgrade to Moq which is a newer framework make GoToMainMenu method virtual (and at least protected) Your test then (Rhino 3.6) should look like...
I wasn't able to find a way to do this with Rhino but realized I could just create a fake myself that would behave the way I expected. This solution turned out to be very readable with proper naming conventions.
Yeah, there is an easier way: test.AssertWasCalled( t => t.CalculateBeta( Arg<double>.Matches(a => Math.Abs(a - 50) < 0.01), Arg<double>.Matches(b => Math.Abs(b - 3.74593228) < 0.000001) ) ); Actually, you can pass any predicate to Arg<T>.Matches() to validate each argument particulary....
c#,unit-testing,mocking,rhino-mocks
You have to call "CallOriginalMethod" method with "OriginalCallOptions" enumeration.(By the way, you use RhinoMocks's old API...) Change your calls to: fakeBoard.Stub(x => x.AddPlayer(x => x.AddPlayer(Arg<Player>.Is.NotNull))) .CallOriginalMethod(OriginalCallOptions.NoExpectation) .Return(true); One more thing, the method "PlayGame" must be a virtual method(to apply this behavior...)...
c#,.net,unit-testing,abstract-class,rhino-mocks
One possible solution is to create an abstract implementation of IA public abstract class A : B, IA { //abstract implementations of all properties and methods on IA, nothing concrete } this can then be stubbed in the unit test: public void Test() { var sut = new MyClass(); var...
c#,unit-testing,tdd,rhino-mocks
I would say mocking HttpCookieCollection is taking things a bit too far - it's just a way of storing cookies - you wouldn't mock an IList<Cookie>, would you? Simply do response.Stub(x => x.Cookies).Return(new HttpCookieCollection()); or similar (not used Rhino Mocks so not sure if this is exactly right)....
c#,unit-testing,mocking,rhino-mocks
What are you actually wanting to test here? That the compiler produces the correct code when you use a using statement? Seems fairly pointeless. Or that your class implements IDisposable? You could do that with a simpler test. Or that your disposal routine correctly disposes of the resources? This test...
c#,asp.net-mvc,unit-testing,rhino-mocks
You have to do it the other way around. Create mocked IFoo and redirect some calls to real IFoo (this has to be done via WhenCalled extension): var realFoo = new Ninject.Kernel(new MyExampleModule()).Get<IFoo>(); var mockFoo = MockRepository.GenerateStub<IFoo>(); mockFoo.Stub(f => f.Method2()).Return(42); mockFoo.Stub(f => f.Method1()) .WhenCalled(invocation => { invocation.ReturnValue = realFoo.Method2(); })...
c#,entity-framework,unit-testing,nunit,rhino-mocks
Few things that should be done better: mock instance can be created via static MockRepository.GenerateMock method (repository instance you are using is old API) DepartmentRepository property should be mocked as Insert call verification will be made mocks.ReplayAll() is not needed call to depService.GetAll() is not needed - in your test...
Maybe this would help, can you give it a try? var mockBLL = MockRepository.GenerateMock<IBLL>(); mockBLL.Stub(x => x.SaveOrUpdateDTO(null, null)) .IgnoreArguments() .Repeat.Twice() // Allow to be called twice .WhenCalled(invocation => { if (nSaveOrUpdateCount > 0) // throw an exception if 2nd invocation throw new Exception(); nSaveOrUpdateCount++; }); SimpleIoc.Default.Register<IBLL>(() => mockBLL); ...