How to unit Test WCF service that calls other WCF - c#

I've setup 2 wcf services which are
PdfService.svc
and
MailService.svc
The MailService attaches a PDF generated by the PDFService ie
public void SendMail(ICommand command)
{
// how should I handle this in Unit Test (mocks with NSubstitute)
_service = new PdfService(_pdfSettings);
var request = new DownloadRequest {FileName = "form.pdf",
FormEntry = command.FormEntry };
var thefile = _service.DownloadFile(request);
sendEmail(command.Mail, thefile.FileByteStream);
}
I would like to know how to stub out the PdfService while testing the MailService, and is this a bad idea for communicating wcf to wcf?
Thanks!

You probably want to use Dependency Injection and Mock Objects.
The general idea is to pass the service to your method in the method call or object constructor. With the snippet you've provided, I'd rewrite it this way:
//Method Call
IPdfService mockService = new MockPdfService() // this is a mock that implements your interface
SendMail(cmd, mockService);
//Method
public void SendMail(ICommand command, IPdfService service)
{
// how should I handle this in Unit Test (mocks with NSubstitute)
_service = new PdfService(_pdfSettings);
var request = new DownloadRequest {FileName = "form.pdf",
FormEntry = command.FormEntry };
var thefile = service.DownloadFile(request);
sendEmail(command.Mail, thefile.FileByteStream);
}
In your mockService you can add diagnostic helps that you can then write Asserts against after the sendmail call to see what happened in your mock.
Probably the better way to do it though is to have your main class constructor take the service object and then rewrite your method call, something like this:
public class MyClass
{
private IPdfService _pdfService;
public MyClass()
{
_pdfService = new PdfService(_pdfSettings);
}
// Call this with your Mock pdfService
public MyClass(IPdfService pdfService)
{
_pdfService = pdfSerivce;
}
public void SendMail(ICommand command)
{
var request = new DownloadRequest { FileName = "form.pdf", FormEntry = command.FormEntry };
var thefile = _pdfService.DownloadFile(request);
sendEmail(command.Mail, thefile.FileByteStream)
}
}

Related

How to mock SOAP client that was auto-generated by Visual Studio?

I have an auto-generated SOAP client. It was generated by visual studio wizard (add connected services -> Microsoft WCF Web Service Reference Provider). I would like to mock that client, so when a method is called, a predefined result is going to be returned in a format of the SOAP response. Unfortunately, I cannot get it to work - my result is either null (instead of defined in .Returns) or I get an exception.
I am trying to apply clean architecture in my design. So this SOAP client landed in my infrastructure layer, where I have created a repository for it. This repository creates DTOs, so they can be dispatched to my persistence. The repository receives the SOAP client through dependency injection. I would also like to have tests for the repository, just to validate that DTOs generation is correct. So, I would like to mock this SOAP service, so I can feed it to the repository and test the returned DTOs.
Auto-generated interface:
public interface ApplicationSoap
{
[System.ServiceModel.OperationContractAttribute(Action = "http://Application/GetAppVersion", ReplyAction = "*")]
Task<ExtApp.GetAppVersionResponse> GetAppVersionAsync(ExtApp.GetAppVersionRequest request);
}
and auto-generated client class:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.1-preview-30514-0828")]
public partial class ApplicationSoapClient : System.ServiceModel.ClientBase<ExtApp.ApplicationSoap>, ExtApp.ApplicationSoap
{
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
System.Threading.Tasks.Task<ExtApp.GetAppVersionResponse> ExtApp.ApplicationSoap.GetAppVersionAsync(ExtApp.GetAppVersionRequest request)
{
return base.Channel.GetAppVersionAsync(request);
}
public System.Threading.Tasks.Task<ExtApp.GetAppVersionResponse> GetAppVersionAsync(int appVer)
{
ExtApp.GetAppVersionRequest inValue = new ExtApp.GetAppVersionRequest();
inValue.Body = new ExtApp.GetAppVersionRequestBody();
inValue.Body.appVer = appVer;
return ((ExtApp.ApplicationSoap)(this)).GetAppVersionAsync(inValue);
}
}
I want to mock ApplicationSoapClient and method GetApplicationVersionAsync.
After different attempts I ended up the following in my test class:
private ExtApp.AppVersion[] _response =
new ExtApp.AppVersion[]
{
new ExtApp.AppVersion
{
VersionNumber = 1,
StartDate = DateTime.Parse("2010-01-01"),
EndDate = DateTime.Parse("2015-12-31")
},
};
private Mock<ExtApp.ApplicationSoap> _client = new Mock<ExtApp.ApplicationSoap>();
public TestClass()
{
var body = new ExtApp.GetAppVersionRequestBody(It.IsAny<int>());
var request = new ExtApp.GetAppVersionRequestRequest(body);
_client
.Setup(s => s.GetAppVersionAsync(request))
.Returns(
Task.FromResult(
new ExtApp.GetAppVersionResponse(
new ExtApp.GetAppVersionResponseBody(_response)))
);
}
[Fact]
public void TestAppVersionDownload()
{
var request = new ExtApp.GetAppVersionRequest(
new ExtApp.GetAppVersionRequestBody(1));
var result = _clientSoap.Object.GetAppVersionAsync(request)
.Result; //returns null instead of defined in Returns section
Assert.True(result.Body.GetAppVersionResult.Length == 2);
}
It runs, but Result of the call is null. I am expecting to get back the object that would have a non-null Body property with the array inside. I am looking for advice how to make this thing work.
Your SOAP client contract is:
public interface ApplicationSoap
{
[System.ServiceModel.OperationContractAttribute(Action = "http://Application/GetAppVersion", ReplyAction = "*")]
Task<ExtApp.GetAppVersionResponse> GetAppVersionAsync(ExtApp.GetAppVersionRequest request);
}
You use this as a dependency in a repository that could look like this:
public class Repository
{
private readonly IApplicationSoap _client;
public Repository(IApplicationSoap client) { _client = client; }
public async Task<AppVersion> GetAppVersionAsync(int version)
{
var request = new GetAppVersionRequest(new GetAppVersionRequestBody(version));
var response = await _client.GetAppVersionAsync(request);
return new AppVersion
{
Version = response.Body.Version,
StartDate = response.Body.StartDate,
EndDate = response.Body.EndDate
};
}
}
In this case you may want to test the code that converts your input to a request and the code that converts the response to your DTO. This is the only code that is yours (as opposed to not being generated by the tools). To do so you need to mock (in fact stub) the SOAP client contract in your Repository test and have it return the response you want:
[Fact]
public async Task GetAppVersionAsync()
{
// arrange
var client = new Mock<IApplicationSoap>(); // mock the interface, not the class!
var result = new AppVersion
{
Version = 1,
StartDate = DateTime.Parse("2010-01-01"),
EndDate = DateTime.Parse("2015-12-31")
};
client.Setup(x => x.GetAppVersionAsync(It.IsAny<GetAppVersionRequest>))
.Returns(Task.FromResult(new GetAppVersionResponse(new GetAppVersionResponseBody(result))));
var repository = new Repository(soapApp);
// act
var dto = await repository.GetAppVersionAsync(1);
// assert (verify the DTO state)
Assert.Equal(1, dto.VersionNumber);
Assert.Equal(new DateTime(2010, 1, 1), dto.StartDate);
Assert.Equal(new DateTime(2015, 12, 31), dto.EndDate);
}
However... just because you can do this it does not mean that you should.

Moq C#, How can I mock this class?

I need to mock this C# WebApi class using the Moq framework
public class PvValuesController
{
private readonly IDataServices dataService;
public PvValuesController(IDataServices dataServices)
{
dataService = dataServices;
}
[HttpGet]
public IHttpActionResult Get(string id, string st, string et)
{
if (dataService == null)
{
return BadRequest("DataService not found");
}
var result = dataService.GetPvData(id, st, et);
return Ok(result);
}
}
Problem is, if I mock it like this:
var controllerMock = new Mock<PvValuesController>();
I'm not passing any DataService to it, so the Get() function will always return a bad request code.
The original line was:
var controller = new PvValuesController(dataService);
Where dataService is a concrete instance of IDataService and is a complex object
How can I effectively mock such class?
EDIT:
my new test code:
[Test]
public async void TestMoq()
{
var a = new List<dynamic>();
a.Add(23);
// arrange
var dataService = new Mock<IDataServices>();
dataService.Setup(l => l.GetPvData(It.IsAny<string>(), It.IsAny<DateTime>(), It.IsAny<DateTime>())).Returns(a);
var controller = new PvValuesController(dataService.Object);
// act
var actionResult = controller.Get("groupid", "timestampstart", "timestampend");
var httpResponseMessage = await actionResult.ExecuteAsync(CancellationToken.None);
// assert
Assert.AreEqual(HttpStatusCode.BadRequest, httpResponseMessage.StatusCode);
}
I get an exception on the await line:
System.InvalidOperationException: HttpControllerContext.Configuration must not be null
Mock your dependency interface as shown below
var service = new Mock<IDataServices>();
Inject it to your controller
var ctrlObj = new PvValuesController(service.Object);
Then continue with your setup as usual for the service
service.SetUp(l => l.Get()).Returns("Hello World!!!");
Finally call your controller method using the controller instance
ctrlObj.Get("A","B","C");

Owin hosted webapi 2.2. testing controller with mocked service

I have webapi which for testing purposes I am hosting in owin. I have set it up using autofac. now when I am testing I want to inject moq dependencies. which I am not able to so far. I have read the documentation and did bit of research but I am missing something.
here is the testing code.
[Test]
public void Request_all_airports()
{
const int port = 8086;
AirportCollection moqAirportCollection = new AirportCollection();
moqAirportCollection.Airports = new List<Airport>{new Airport{IATA = "moq",Name = "moqName"}};
using (WebApp.Start<Startup>("http://localhost:" + port))
{
using (var mock = AutoMock.GetLoose())
{
var moqObj = mock.Mock<IAirportService>().Setup(x => x.GetAirports()).Returns(moqAirportCollection);
var client = new HttpClient {BaseAddress = new Uri("http://localhost:" + port)};
var response = client.GetAsync("/api/airport/get").Result;
var body = response.Content.ReadAsStringAsync().Result;
var airportCollection = JsonConvert.DeserializeObject<AirportCollection>(body);
}
}
}
Please have a look. let me know what I am missing. if you want to look at controller code or any other piece do let me know .
here is code for startup
public class Startup
{
public static IContainer container { get; set; }
public void Configuration(IAppBuilder appBuilder)
{
var httpConfig = new HttpConfiguration();
container = AutofacSetup.Register(httpConfig);
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
Thanks
I think so I have solved it with help from people. here is my code.
var moq = new Mock<IAirportService>();
moq.Setup(x => x.GetAirports()).Returns(moqAirportCollection);
newBuilder.RegisterInstance(moq.Object).As<IAirportService>();
newBuilder.Update(Startup.container);
I havnt rebuild the contrain i just updated it. autofac have behavior to use latest registration so it will use mocked on here.
You are almost there.
In your test you need to register your mock service with your autofac container so that dependencies on IAirportService are resolved with the mock in the application.
One way to achieve this is override the Startup class' Configuration method for each test and put your test DI in there. I've put some comments below to show changes that can be made:
public class Startup
{
public static IContainer container { get; set; }
// make this virtual
public virtual void Configuration(IAppBuilder appBuilder)
{
var httpConfig = new HttpConfiguration();
// have this return the ContainerBuilder instead of the container
var builder = AutofacSetup.Register(httpConfig)
container = builder.Build();
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
Then in your test class, derive from the Startup class and put your test logic in. Something like this:
public class MyTestCase {
public static Mock<IAirportService> MockObj { get; set; }
private class TestStartup : Startup {
public override void Configuration(IAppBuilder app) {
var httpConfig = new HttpConfiguration();
// this now returns ContainerBuilder instead of the container
var builder = AutofacSetup.Register(httpConfig)
// register your mock, change this to whatever lifetime scope you need
var moqAirportCollection = new AirportCollection();
moqAirportCollection.Airports = new List<Airport>{new Airport{IATA = "moq",Name = "moqName"}};
var mock = AutoMock.GetLoose()
MockObj = mock.Mock<IAirportService>()
.Setup(x => x.GetAirports())
.Returns(moqAirportCollection);
var moqObj = MockObj.Object;
builder.RegisterInstance(moqObj).As<IAirportService>();
container = builder.Build();
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
[Test]
public void Request_all_airports()
{
using (var server = WebApp.Start<Startup>())
{
var response =
server.CreateRequest("/api/airport/get")
.GetAsync()
.Result;
var body = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<AirportCollection>(body);
// assert something
}
}
}
A unit test should test a single component. In your case, you are trying to test the AirportController through a HTTP query, not the AirportController as a standalone component.
The AirportController class depends on a IAirportService component. In order to test the component without any dependency you created a moq on IAirportService. Now you can instantiate a new AirportController with this moq and run your test using this instance.
If you have a AirportController like this
public class AirportController
{
public AirportController(IAirportService airportService) { /* ... */}
}
The AirportController test should be like this :
[Test]
public void Request_all_airports()
{
AirportCollection moqAirportCollection = new AirportCollection();
var moqAirPort = new Airport{ IATA = "moq",Name = "moqName" };
moqAirportCollection.Airports = new List<Airport>{ moqAirPort };
using (var mock = AutoMock.GetLoose())
{
var moqAirportService = mock.Mock<IAirportService>()
.Setup(x => x.GetAirports())
.Returns(moqAirportCollection);
var testedAirportController = new AirportController(moqAirportService);
AirportCollection airportCollection = testedAirportController.Get();
Assert.AreEquals(1, airportCollection.Length, "Invalid number of airport");
Assert.AreEquals(moqAirPort.Name, airportCollection[0].Name, "Invalid name");
}
}

How could I Mock this code?

I want to start mocking some code but I am unsure how to do it. I have read a few tutorials but I cannot apply it to my code (There maybe a reason behind it).
I am using NUnit and Moq.
(I have removed all other methods so I can just show you one).
Api Interface:
public interface IApiRequest
{
Task<T> ExecuteAsync<T>(RestRequest request);
}
Method I want to Mock:
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
var api = new ApiRequest();
return await api.ExecuteAsync<UpcomingMovies>(request);
}
I'm not sure how I can mock this.
Update:
Is this now a valid test?
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(new UpcomingMovies()));
If you want to mock the ExecutAsync method you can do it like this:
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(/** whatever movies **/));
if you want to mock for a particlur request, replace It.IsAny<RestRequest>() with a reference to your request.
To effectively test your class you need something like this:
public class MyClass
{
public MyClass(IApiRequest api)
{
this.api = api;
}
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
return await api.ExecuteAsync<UpcomingMovies>(request);
}
}
Test
[Test]
public async Task MyTest()
{
var expectedMovies = new UpcomingMovies(); // or whatever movies you need
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(expectedMovies));
var myClass = new MyClass(mock.Object);
var result = await myClass.GetUpcomingMovies(1);
Assert.IsTrue(expectedMovies == result);
}

In ServiceStack is it possible to mock the Request.OriginalRequest object for unit tests?

I'd like to make my ServiceStack service testable.
Presently I have:
[RequireFormsAuthentication]
public object Delete(DeleteRequest request)
{
var originalRequest = (HttpRequest)Request.OriginalRequest;
var identity = originalRequest.RequestContext.HttpContext.User.Identity;
return othercode(identity);
}
Where RequireFormsAuthentication is
public class RequireFormsAuthenticationAttribute : RequestFilterAttribute
{
public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto)
{
var originalRequest = (HttpRequest)req.OriginalRequest;
var identity = originalRequest.RequestContext.HttpContext.User.Identity;
if (!identity.IsAuthenticated)
{
res.StatusCode = (int)HttpStatusCode.Forbidden;
res.EndServiceStackRequest(skipHeaders: true);
}
}
}
I've mocked out all the dependencies used by 'othercode()' and all that's left is the stuff that's in the base class Service. Is there a pattern/strategy/approach/something I'm missing that makes this trivial?
Here's how to test with Moq. This service looks for a "key" and "value" in the query string and another parameter in the request DTO. The service returns a string response based on the value given.
[Test]
public void MyTest()
{
var mockRequestContext = new Mock<IRequestContext>();
var mockedHttpRequest = new Mock<IHttpRequest>();
NameValueCollection querystring = new NameValueCollection();
querystring.Add("myKey", "myValue");
mockedHttpRequest.SetupGet(r => r.QueryString).Returns(querystring);
mockRequestContext.Setup(x => x.Get<IHttpRequest>()).Returns(mockedHttpRequest.Object);
AboutService service = new AboutService
{
RequestContext = mockRequestContext.Object,
};
AboutResponse response = (AboutResponse)service.Any(new About
{
Company = "myOtherValue",
});
Assert.AreEqual(0, response.QueryResult);
Assert.AreEqual("validResponse", response.Version);
}
I apologize for not using moq...already had some of this done using RhinoMocks. I think the concept should transfer to moq. This might be a good resource as well as this this.
Anyway, I think the test code below should get you started. Your seam into mocking Request.OriginalRequest is replaceing the Service.RequestContext with a mock object. Then you just have to mock everything beyond that. It's going to be a lot of 'mocking' and if you repeat to yourself 'Are you mocking me' every time you mock a class it's almost enjoyable.
[Test]
public void testsomethign()
{
var mockedRequestContext = MockRepository.GenerateMock<IRequestContext>();
var mockedHttpRequest = MockRepository.GenerateMock<IHttpRequest>();
var mockedOriginalRequest = MockRepository.GenerateMock<HttpRequestBase>();
var mockedOriginalRequestContext = MockRepository.GenerateMock<RequestContext>();
mockedOriginalRequest.Stub(x => x.RequestContext).Return(mockedOriginalRequestContext);
mockedHttpRequest.Stub(x => x.OriginalRequest).Return(mockedOriginalRequest);
mockedRequestContext.Stub(x => x.Get<IHttpRequest>()).Return(mockedHttpRequest);
var service = new ServiceTests()
{
RequestContext = mockedRequestContext
};
service.Delete(new DeleteRequest());
}
Be sure to check out the namespace: ServiceStack.ServiceInterface.Testing.
In there you can find a MockRequestContext that you can use as follows:
var mockContext = new ServiceStack.ServiceInterface.Testing.MockRequestContext();
//do stuff to set it up if desired...
AboutService service = new AboutService
{
RequestContext = mockContext
};

Categories

Resources