rhino-mocks

How do I use Rhino.Mocks to mock a ControllerContext

半腔热情 提交于 2019-11-30 21:59:21
I am trying to use Rhino.Mocks to mock up a ControllerContext object to gain access to runtime objects like User, Request, Response, and Session in my controller unit tests. I've written the below method in an attempt to mock up a controller. private TestController CreateTestControllerAs(string userName) { var mock = MockRepository.GenerateStub<ControllerContext>(); mock.Stub(con => con.HttpContext.User.Identity.Name).Return(userName); mock.Stub(con => con.HttpContext.Request.IsAuthenticated).Return(true); var controller = CreateTestController(); // left out of example for brevity controller

Ordering method return values with Rhino-Mock stubs

懵懂的女人 提交于 2019-11-30 19:28:12
I've started experimenting with Rhino-Mocks (3.6) while reading Roy Osherove's The Art of Unit Testing . He has an example that demonstrates that a mocked method can be scripted to return different results when called twice with the same parameter: [Test] public void ReturnResultsFromMock() { MockRepository repository = new MockRepository(); IGetRestuls resultGetter = repository.DynamicMock<IGetRestuls>(); using(repository.Record()) { resultGetter.GetSomeNumber("a"); LastCall.Return(1); resultGetter.GetSomeNumber("a"); LastCall.Return(2); resultGetter.GetSomeNumber("b"); LastCall.Return(3); }

What is the AAA syntax equivalent to using Ordered() in Rhino Mocks

时光怂恿深爱的人放手 提交于 2019-11-30 18:43:12
I can't for the life of me find the proper syntax using the Fluent/AAA syntax in Rhino for validating order of operations. I know how to do this with the old school record/playback syntax: MockRepository repository = new MockRepository(); using (repository.Ordered()) { // set some ordered expectations } using (repository.Playback()) { // test } Can anyone tell me what the equivalent to this in AAA syntax for Rhino Mocks would be. Even better if you can point me to some documentation for this. tvanfosson Try this: // // Arrange // var mockFoo = MockRepository.GenerateMock<Foo>(); mockFoo

How do I use Rhino.Mocks to mock a ControllerContext

匆匆过客 提交于 2019-11-30 17:17:51
问题 I am trying to use Rhino.Mocks to mock up a ControllerContext object to gain access to runtime objects like User, Request, Response, and Session in my controller unit tests. I've written the below method in an attempt to mock up a controller. private TestController CreateTestControllerAs(string userName) { var mock = MockRepository.GenerateStub<ControllerContext>(); mock.Stub(con => con.HttpContext.User.Identity.Name).Return(userName); mock.Stub(con => con.HttpContext.Request.IsAuthenticated)

RhinoMocks - Not specifying all parameters in AssertWasCalled

守給你的承諾、 提交于 2019-11-30 17:11:05
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<ISomeInterface>(); var myObj = new MyClass(myMock); myObj.foo() myMock.AssertWasCalled(factory => factory

How to Construct IdentityResult With Success == true

一笑奈何 提交于 2019-11-30 16:57:55
I have a class with Microsoft.AspNet.Identity.UserManager injected, and I want to expect the userManager.CreateAsync(user, password) method to return a Task where the IdentityResult.Succeeded = true. However, the only available constructors for IdentityResult are failure constructors that will cause Succeeded property to be false. How does one create an IdentityResult that has Succeeded == true? IdentityResult doesn't implement an interface and Succeeded isn't virtual so I don't see any obvious ways of creating a mock object through Rhino Mocks (which i'm using as my mocking framework). My

RhinoMock : Mocks Vs StrictMocks Vs DynamicMocks

南笙酒味 提交于 2019-11-30 13:40:06
问题 I understand the difference between a Mock and a Stub. But different types of Mocks in RhinoMock framework confuses me. Could someone explain the concepts of Mocks Vs StrictMocks Vs DynamicMocks in terms of RhinoMock framework. your answers are greatly appreciated. 回答1: A strict mock is a mock that will throw an exception if you try to use any method that has not explicitly been set up to be used. A dynamic (or loose) mock will not throw an exception if you try to use a method that is not set

Has anyone successfully mocked the Socket class in .NET?

和自甴很熟 提交于 2019-11-30 13:01:38
I'm trying to mock out the System.net.Sockets.Socket class in C# - I tried using NUnit mocks but it can't mock concrete classes. I also tried using Rhino Mocks but it seemed to use a real version of the class because it threw a SocketException when Send(byte[]) was called. Has anyone successfully created and used a Socket mock using any mocking framework? Whenever I run into these kinds of problems with Moq I end up creating an interface to abstract away the thing I can't mock. So in your instance you might have an ISocket interface that implements the Send method. Then have your mocking

Stubbing or Mocking ASP.NET Web API HttpClient

前提是你 提交于 2019-11-30 11:54:40
问题 I am using the new Web API bits in a project, and I have found that I cannot use the normal HttpMessageRequest , as I need to add client certificates to the request. As a result, I am using the HttpClient (so I can use WebRequestHandler ). This all works well, except that it isn't stub/mock friendly, at least for Rhino Mocks. I would normally create a wrapper service around HttpClient that I would use instead, but I would like to avoid this if possible, as there are a lot of methods that I

Rhino Mocks stubs and mocks are only good for interfaces?

心已入冬 提交于 2019-11-30 11:02:10
Is it correct that Rhino Mocks stubs and mocks are only good for interfaces, not concrete classes? I spent quite a time trying to make this piece of code working. I did not expect the stubbed pubSubClient to always call Send method from the class. That method has some dependencies and throws exception. [Test] public void Test01() { PubSubMessage psm = new PubSubMessage(); var pubSubClient = MockRepository.GenerateStub<PubSubClient>(); pubSubClient.Stub(x => x.Send(psm)).IgnoreArguments().Return(null); // actual PubSubClient Send method throws exception // the rest of the test is skipped... }