I got a mvc 3 project where I want to mock HttpResonseBase and HttpRequestBase. Im using RhinoMocks 3.6 to mock myobjects. My test code rightnow looks like this.
[TestMethod]
public void Test()
{
MockRepository repo = new MockRepositoy();
HttpContextBase mockHttpContext= repo.StrictMock<HttpContextBase>();
HttpRequestBase mockRequest = repo.StrictMock<HttpRequestBase>();
HttpResponseBase mockResponse = repo.StrictMock<HttpResponseBase>();
ICookie mockCookie = repo.StrictMock<ICookie>();
Controller instanceToTest = new Controller(mockCookie);
SetupResult.For(mockHttpContext.Request).Return(mockRequest);
SetupResult.For(mockHttpContext.Response).Return(mockResponse);
mocks.Replay(context);
instanceToTest.ControllerContext = new ControllerContext(mockHttpContext, new RouteData(), instanceToTest);
mockCookie.Expect(x=>x.MethodToExpect("Test",mockRequest,mockResponse);
mockRepository.ReplayAll();
instanceToTest.MethodToTest();
mockRepository.VerifyAll();
}
When im running the test I get this errormessage;
Rhino.Mocks.Exceptions.ExpectationViolationException: ICookie.MethodToExpect("Test", System.Web.HttpResponseBase, System.Web.HttpRequestBase); Expected #0, Actual #1.
ICookie.MethodToExpect("Test", HttpResponseBaseProxy); Expected #1, Actual #0.
What am I doing wrong?
The problem here is that you use StrictMock - it means that if you call a method on the Mock object that you haven't set any expectations on it, VerifyAllExpectations will fail. You could use MockRepository.GenerateMock<T> method instead of the StrictMock.
Another comment is that you'd better stick with the RhinoMocks AAA syntax (using the Expect, Stub and VerifyAllExpectations methods instead of ReplayAll, SetupResult etc...)
Here's how your code may look like with pure AAA syntax:
[TestMethod]
public void Test()
{
// Arrange(A) - create your objects, mocks and stubs
// The context is a Stub - you just want it to return the mocked request and response
HttpContextBase mockHttpContext= MockRepository.GenerateStub<HttpContextBase>();
HttpRequestBase mockRequest = MockRepository.GenerateMock<HttpRequestBase>();
HttpResponseBase mockResponse = MockRepository.GenerateMock<HttpResponseBase>();
ICookie mockCookie = MockRepository.GenerateMock<ICookie>();
Controller instanceToTest = new Controller(mockCookie);
// Stub will return the mocked request and response on every call (similar to SetupResult)
mockHttpContext.Stub(x => x.Request).Return(mockRequest);
mockHttpContext.Stub(x => x.Response).Return(mockResponse);
instanceToTest.ControllerContext = new ControllerContext(mockHttpContext, new RouteData(), instanceToTest);
mockCookie.Expect(x=>x.MethodToExpect("Test",mockRequest,mockResponse);
// Act(A) - do the actual operations on the tested class
instanceToTest.MethodToTest();
// Assert (A) - Verify your expectations
mockCookie.VerifyAllExpectations();
}
Related
I have the following function that I am trying to put under unit test
public MessageListDto GetMessageList(SimpleSearchCriteria criteria)
{
var messages = _repository.GetMessages(criteria, out int total);
return new MessageListDto(messages, total);
}
and the following is my test so far, in which I am able to determine that _repository.GetMessages is call with the correct parameters.
However, how do I test that the second line is tested properly, I need to test that
A new object of type MessageListDto is constructed with the two parameters returned from the line above
the newly constructed object is returned
[Test]
public void Test1()
{
int total = 10;
var searchCriteria = new SimpleSearchCriteria();
var mockRepo = new Mock<IMessageRepository>();
var messageService = new MessageService(mockRepo.Object);
messageService.GetMessageList(searchCriteria);
mockRepo.Verify(r => r.GetMessages(searchCriteria, out total), Times.Once);
mockRepo.Verity ??????????
}
As we have discussed in the comment section your goal is to test the returned messages is passed to the dto's constructor without any modification.
With your current code you can't do that because you are creating the DTO explicitly inside your method. So, you can't replace it with a spy.
Just to clarify some terminologies:
Dummy: simple code that returns bogus data
Fake: a working alternative which can take shortcuts
Stub: custom logic with predefined data
Mock: custom logic with expectations (interactive stub)
Shim: custom logic at run-time
Spy: interceptors to record calls
To be able to capture the arguments of the MessageListDto constructor call you need to introduce an other layer between your GetMessageList method and the constructor. (In other words indirection)
For the sake of simplicity let me assume that IMessageRepository's GetMessages returns a string array.
So, you can introduce an interface like this:
public interface IDtoProvider
{
public MessageListDto CreateMessageList(string[] messages, int total)
=> new MessageListDto(messages, total);
}
Here I have used C# 8's interface default implementation feature
It can be further shortened with C# 9's target-typed new expression feature
If you are using older C# version then you need to separate the interface and the default implementation from each other
Now lets amend your MessageService to receive a IDtoProvider instance via its constructor
class MessageService
{
private readonly IMessageRepository _repository;
private readonly IDtoProvider _dtoProvider;
public MessageService(IMessageRepository repository, IDtoProvider dtoProvider)
=> (_repository, _dtoProvider) = (repository, dtoProvider);
public MessageListDto GetMessageList(SimpleSearchCriteria criteria)
{
var messages = _repository.GetMessages(criteria, out int total);
return _dtoProvider.CreateMessageList(messages, total);
}
...
}
Here I've took advantage of ValueTuple's deconstruct capability
With these in our hand we can write a unit test like this:
//Arrange - Repo
string[] messages = new[] { "OnlyElement" };
int total = 10;
var mockRepo = new Mock<IMessageRepository>();
mockRepo
.Setup(repo => repo.GetMessages(It.IsAny<SimpleSearchCriteria>(), out total))
.Returns(messages);
//Arrange - Provider
var dto = new MessageListDto(messages, total);
string[] messagesArgument = null;
int totalArgument = -1;
var mockProvider = new Mock<IDtoProvider>();
mockProvider
.Setup(provider => provider.CreateMessageList(It.IsAny<string[]>(), It.IsAny<int>()))
.Callback<string[], int>((messages, total) => (messagesArgument, totalArgument) = (messages, total))
.Returns(dto);
//Arrange - SUT
var searchCriteria = new SimpleSearchCriteria();
var sut = new MessageService(mockRepo.Object, mockProvider.Object);
//Act
sut.GetMessageList(searchCriteria);
//Assert - Repo
mockRepo.Verify(r => r.GetMessages(searchCriteria, out total), Times.Once);
//Assert - Provider
Assert.Equal(messages, messagesArgument);
Assert.Equal(total, totalArgument);
I've defined how should the IMessageRepository mock behave
I've defined how should the IDtoProvider mock behave
2.1 I've used here the Callback method to capture the calling argument
2.2 If you perform multiple calls against your mocked method then please consider to use Capture.In
I've instantiated a MessageService (which is our system under test) with the mock objects
I've called the GetMessageList inside the Act phase
I've made assertion against the repo mock call
I've made assertion against the calling parameters of the CreateMessageList
6.1 Here I have used xunit's assertion
The first step is to setup the MessageService mock, so that it returns something deterministic and then in the second step you verify that that has been used to construct your MessageListDto.
[Test]
public void Test1()
{
// arrange
int total = 10;
var searchCriteria = new SimpleSearchCriteria();
var messages = new [] {"message1", "message2"} // or similar
var mockRepo = new Mock<IMessageRepository>();
// or similar, I am not quite certain as to the specific syntax. Especially wrt. out parameters. Check the documentation.
mockRepo.Setup(x => x.GetMessages(It.IsAny<SimpleSearchCriteria>(), It.IsAny<int>())).Returns(messages);
var messageService = new MessageService(mockRepo.Object);
// act
var result = messageService.GetMessageList(searchCriteria);
// assert
mockRepo.Verify(r => r.GetMessages(searchCriteria, out total), Times.Once);
// or similar; here you might want to check out FluentAssertions as #Peter Csala suggested
Assert.Equal(result.Messages, messages);
}
What's missing from previous answers is that whenever possible, you should set up the mocked method with the exact parameters, that you expect it to receive. In other words set up the method with concrete values instead of It.IsAny<>(). In such case you won't have to verify the method later at all. (Unless it's critical to test, how many times a method is called). Simply, if mocked GetMessages receives different arguments than expected, it will return null, set total to 0 and your test will fail. Having the mocked methods set up properly, you can now focus on verifying what GetMessageList returns, which is the purpose of this unit test.
[Test]
public void Test1()
{
int total = 10;
var messages = new[] {new Message()};
var searchCriteria = new SimpleSearchCriteria();
var mockRepo = new Mock<IMessageRepository>();
mockRepo.Setup(_ => _.GetMessages(searchCriteria, out total))
.Returns(messages);
var messageService = new MessageService(mockRepo.Object);
var dto = messageService.GetMessageList(searchCriteria);
Assert.AreSame(messages, dto.Messages);
Assert.AreEqual(total, dto.Total);
}
I am new to this MOQ framework and honestly having a hard time with having to get my unit test to run. Basically, I have a C# application which basically does some uploads to APIs using PostAsync.
Now, since I can't (and should not) call the API during my unit test (as otherwise it would be an integration test), I added a wrapper method around it and allowing that method to return true by mocking it. But no matter what I do, it is returning false. I have gone through SO questions, but I am not sure what am I missing. I haven't used interfaces but am using classes with virtual methods.
Here is my sample code that I would want to test
public async Task<bool> CreateNoteBookDirectory (string url ,string bearertoken, JavaScriptSerializer jser,RestPostClass rest)
{
NoteBookDirectory jsnbdir = new NoteBookDirectory();
jsnbdir.path = "/JobNotebooks/ClientScoreDataInput";
var directorycreate = jser.Serialize(jsnbdir);
var content = new StringContent(directorycreate, Encoding.UTF8, #"application/json");
bool result=await rest.HttpPost(url, content, bearertoken);
return result;
}
This method is in the main class.
The RestPostClass class has the virtual method HttpPost, whose skeleton is somewhat like this
public async virtual Task<bool> HttpPost(String url, StringContent content, string bearertoken)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearertoken);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(#"application/json"));
var postresult = await client.PostAsync(url, content);
bool result = parseResponse(postresult);
return result;
}
Now, in my unit test, I am trying to test the CreateNoteBookDirectory method and since do not want the post method to be called, mocking it.
Here is what I am doing in my sample unit test
Mock<DataBricksRestPost> mock = new Mock<DataBricksRestPost>();
mock.Setup(x => x.HttpPost("http://test.com", new StringContent("abc"), "token")).Returns(Task.FromResult(true));
Program prog = new Program();
var jser = new JavaScriptSerializer();
bool result= await prog.CreateNoteBookDirectory("http://test.com", "token", jser, mock.Object);
Assert.IsTrue(result, "Test failed");
It keeps returning false because apparently the mocking does not really happen properly.
What am I missing?
Any questions and I will try my best to clarify.
P.S: I have used the existing the "program" class as I am basically starting.
Mock returns false, because when you call HttpPost parameters don't match with ones that were set up. The second parameter is different.
You can set up mock like that:
mock
.Setup(x => x.HttpPost("http://test.com", It.IsAny<StringContent>(), "token"))
.Returns(Task.FromResult(true)); //or .ReturnsAsync(true);
It tells mocking framework, that second parameter can be any object of type StringContent.
Docs can be found here: https://github.com/Moq/moq4/wiki/Quickstart#matching-arguments
I am new to MS Unit Testing and Moq objects. I am trying to test my Web API 2 controller. I have given below my unit test and controller code. While stepping through the code, it doesn't even go to the GetDeliveryCodeStrategy method.
[TestMethod]
public void CreateDelivery_ShouldReturnDeliveryCode()
{
Mock<IDeliveryStrategy> deliveryStrategy = new Mock<IDeliveryStrategy>
();
Mock<IDeliveryCode> deliveryCode = new Mock<IDeliveryCode>();
var controller = new DeliveryCodeController(deliveryStrategy.Object,
deliveryCode.Object);
var controllerContext = new HttpControllerContext();
var request = new HttpRequestMessage();
request.Headers.Add("appToken", "a57ffa87-950e-40f4-b965-17788becac7d");
controllerContext.Request = request;
controller.ControllerContext = controllerContext;
var result = controller.CreateDelivery(50) as
CreatedNegotiatedContentResult<IDeliveryCode>;
Assert.IsNotNull(result);
}
public class DeliveryCodeController : ApiController
{
IDeliveryStrategy _deliveryBatch;
IDeliveryCode _deliveryCode;
//Constructor dependency injection through Autofac
public DeliveryCodeController(IDeliveryStrategy DeliveryBatch,
IDeliveryCode deliveryCode)
{
_deliveryBatch = DeliveryBatch;
_deliveryCode = deliveryCode;
}
[HttpPost]
[Route("api/DeliveryCode/{percentage}")]
public IHttpActionResult CreateDelivery(int percentage)
{
String appToken = String.Empty;
if (Request.Headers.TryGetValues("appToken", out IEnumerable<String>
headerValues))
{
appToken = headerValues.FirstOrDefault();
}
if (!String.IsNullOrEmpty(appToken)))
{
IDeliveryContext deliveryContext =
_deliveryBatch.GetDeliveryCodeStrategy(percentage);
_deliveryCode.Code = deliveryContext.Create();
return Created(Request.RequestUri.ToString(), _deliveryCode);
}
else
{
return Content(HttpStatusCode.Forbidden, new Error { message = "The App
Token is not valid." });
}
}
}
When I do the "Debug Test" and step through the code, the deliveryContext
object comes as null in the code IDeliveryContext deliveryContext =
_deliveryBatch.GetDeliveryCodeStrategy(percentage);
You have to set up the Mock to return a certain value:
IDeliveryContext deliveryContext = // ???? - whatever you want it to be.
// Could be another Mock.
// This is what the Mock will return.
Mock<IDeliveryStrategy> deliveryStrategy = new Mock<IDeliveryStrategy>();
deliveryStrategy.Setup(x => x.GetDeliveryCodeStrategy(It.IsAny<decimal>()))
.Returns(deliveryContext);
This tells the Mock that that when its GetDeliveryCodeStrategy method is called, it should return the specified IDeliveryContext. Depending on what you're trying to do, that could be another Mock. (Mocks that return mocks are undesirable, but if you're starting out I'd file that detail away and come back to it.)
I'm guessing that percentage is a decimal. It.IsAny<decimal>() means that the mock doesn't care what the value is. That's usually okay because what you're testing is what your class does with the object returned by the mock.
You need to call Setup() on mock objects for the methods that you want to use:
var deliveryStrategy = new Mock<IDeliveryStrategy>();
deliveryStrategy.Setup(x => x.GetDeliveryCodeStrategy(It.IsAny<int>))
.Returns(AMockOfDeliveryContext); //you need to mock it beforehand so you can
//use the object here
I want to assert that a call on my real object (system under test) was called. Here is my test
// Arrange
var contextFactory = A.Fake<IContextFactory>();
var db = A.Fake<IDatabase>();
A.CallTo(() => contextFactory.GetContext()).Returns(db);
var vm = new MainViewModel(contextFactory);
// Act
vm.Loaded();
// Assert
A.CallTo(() => vm.LoadModels(db)).MustHaveHappened();
I'm getting an ArgumentException that says "The specified object is not recognized as a fake object." How do I test that the LoadModels() method in my MainViewModel gets called from the Loaded() method?
EDIT
The reason I'm doing it this way is because the Loaded() method calls a bunch of other methods when the app starts and I don't want to have to setup all the other stuff for each test, I just want to make sure that all the proper methods get called and then test them individually. I'm open to suggestion for a better way of going about this.
Here are the Loaded and LoadModels methods
internal virtual void Loaded()
{
using (var db = _contextFactory.GetContext())
{
LoadModels(db);
// bunch of other method calls
}
}
internal virtual void LoadModels(IDatabase db)
{
Models = new ObservableCollection<Model>(db.Models);
}
It looks like you're verifying a method (vm.LoadModels) that isn't part of the fake (db). You can only verify methods on a fake, not methods that happen to take the fake as an argument.
I ended up moving the functionality of LoadModels() to another class that implements IStartupDataLoader and then I was able to test it like this
// Arrange
var sdl = A.Fake<IStartupDataLoader>();
var exp = A.Fake<ObservableCollection<Model>>();
A.CallTo(() => sdl.LoadModels()).Returns(exp);
var sut = new MainViewModel(sdl);
// Act
sut.Loaded();
// Assert
Assert.That(exp == sut.Models);
my code does this
string domainUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
int result = string.Compare(domainUrl, "http://www.facebookmustdie.com");
// do something with the result...
How can I mock this so my unit test passes, do I have to mock the whole HttpContext class? And if that was the case how would I inject that into the code so the 'correct' HttpContext is used when the unit test is run
You don't need to mock it:
var sb = new StringBuilder();
TextWriter w = new StringWriter(sb);
var context = new HttpContext(new HttpRequest("", "http://www.example.com", ""), new HttpResponse(w));
HttpContext.Current = context;
Console.WriteLine(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority));
The HttpContext singleton can't be easily mocked, and don't play well with unit testing either, because it ties you with the IIS infrastructure.
It's true that Moles can do the job too, but the true issue is the heavy couplage with IIS.
You should rather pass the relevant request data (url for exemple in your case) to your function or class, in order to be able to isolate your logic from the infrastructure. This way, you will be able to separate it from IIS, and run it on a testing infrastructure easily.
Basically, there are two possibilities:
Use TypeMock Isolator or Moles to mock HttpContext.
Introduce an interface for the whole HttpContext class or just for a UrlHelper and pass an instance of a class that implements that interface into your class. Keyword: Dependency Injection (DI)
I don't know what type of project you're writing this code for, but if it's an MVC project I'd suggest re-writing your code to use HttpContextBase instead - if that's possible. Then you can create a stub or mock of this and inject that for your tests. Request.Url returns System.Uri so you'll have to create an instance of this and set that on your context stub/mock.
Example for above :
namespace Tests
{
[TestClass()]
public class MyTests
{
[ClassInitialize()]
public static void Init(TestContext context)
{
// mock up HTTP request
var sb = new StringBuilder();
TextWriter w = new StringWriter(sb);
var httpcontext = new HttpContext(new HttpRequest("", "http://www.example.com", ""), new HttpResponse(w));
HttpContext.Current = httpcontext;
}
[TestMethod()]
public void webSerivceTest()
{
// the httpcontext will be already set for your tests :)
}
}
}