MvcContrib TestHelpers don't seem to provide a full HTTPContext.Server object - rhino-mocks

So the MvcContrib TestHelpers create mock versions of the following
HttpContext
HttpRequest
HttpResponse
HttpSession
Form
TempData
QueryString
ApplicationPath
PathInfo
within a fake controller when using this kind of code
var _controller = new FooController();
var _builder = new TestControllerBuilder();
_builder.InitializeController(_controller);
But you'll notice they don't create a fake/mock Server object. Presumably there is a good reason why not. But I'm trying to stub out the Server.MapPath() method because the method in my SUT uses it and of course during the test its returning NULL.
The TestHelpers use rhino mocks (v3.5) and so am I. I know the syntax to stub out a method but how do I get the Server fake/mock object into my controller so I can stub out the method?

The HttpServerUtility class is sealed, but Microsoft provides an abstract HttpServerUtilityBase which can be used for mocking (and which MVCContrib uses). Simon's problem is due to a bug in MVCContrib. See: Trying to stub Server.MapPath with MvcContrib Test helpers and Rhino Mocks 3.5 for a solution.

Related

Simple Injector - register HttpClient

I have been having issues registering HttpClient in my project. How can I do this?. I injected HttpClient in multiple classes and I need to register it to work for the multiple constructors.
It will have error for using container.Register<HttpClient>(); directly when I test.
System.ArgumentException: 'For the container to be able to create HttpClient it should have only one public constructor: it has 3
I find a workaround based on using below code:
container.Register<HttpClient>(()=>new HttpClient(),Lifestyle.Scoped);
based on
Registering a type with multiple constructors and string dependency in Simple Injector
However, I advice that you use the built-in IHttpClientFactory in asp.net core:
The IHttpClientFactory can be registered by calling the services.AddHttpClient() in Startup.ConfigureServices method.
services.AddHttpClient();
https://learn.microsoft.com/nl-nl/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.0#basic-usage

How to access TestContext in Specflow 3.0 [BeforeTestRun] static Hook

I have started using Specflow 3.0 for .NET core tests project with built-in MSTest runner.
I encountered problem while trying to setup one-time method execution in order to persist bearer token value for all running tests in Specflow.
So my idea was to create [BeforeTestRun] Hook static method which would make HTTP request with given credentials. The problem is that those credentials are stored in .runsettings file, so I need to have MSTest's TestContext object in order to access dictionary property.
In Scenario's I'm injecting TestContext and it works fine, however I do not know how to access this object in static [BeforeTestRun] method. Is there any chance to create this object myself and store in static property?
As you see this is abstract class, so I guess Test Framework is automatically injecting concrete implementation in Scanarios with DI. How could I get this concrete implementation?
If solution is not present, would you suggest another approach how could I store configurable settings besides .runsettings so I could access them in static [BeforeTestRun] method without DI?
Thank you
AFAIK I know the behaviour of TestContext in MSTest, you get in plain MSTest for every test your own instance of TestContext.
So you can't get an instance for the whole testrun.
What you could do is, that you do the HTTP request in the BeforeScenario hook of the first scenario that gets executed. There you can get to the TestContext instance without problems.

asp.net core dependency injection issue - AddScoped not creating a new instance

I've been using asp.net core dependency injection and I have seen an not expected behavior, at least for me. I'm adding a new service to the container like this:
services.AddScoped<IMyClass>(provider =>
{
return new MyClass(
"anyValue"
});
After that, I inject the class into another class to use it:
public class AnotherClass(IMyClass xxx){
}
The thing is that there are a couple configurations that are made on the MyClass constructor based on request information. The problem is that I've seen the MyClass constructor be executed at the application startup only. After that, the class seems to use the same instance for all calls. As I'm using Scoped service I'm expecting to have a new instance for each request, am I wrong?
Thanks.
Ok. The problem was that the class that was receiving the injection was added to the container as singleton. I just changed it to Scoped and everything worked well.
Thanks!

Inject Mocking using TypeMock

I using Ioc pattern(Ninject) in my application. I want make a test case (using TypeMock) to testing Ioc pattern(Ninject). but I don't know how to mock object which creating using Ninject. Kindly let me know how can I inject mock or inject dependencies using TypeMock. Thanks
I'm not sure what you're testing but you might be interested in using Isolate.Swap
Just create a fake object and then call Swap so that the next instance created shall be a fake one - no need for Ioc:
var fakeObject = Isolate.Fake.Instace<MyObject>();
Isolate.Swap.NextInstace<MyObject>().With(fakeObject);
// Call code under test
The first object of type MyObject created after this code will be a fake object.
If you do want to use Ioc to inject your fake object you need to be able to set it to return the object created using Isolate.Fake.Instace.

How to unit test a WCF Client built with WCF Rest Starter Kit

I created some POX services using the REST Starter kit.
At first, it was a little complicated to create the unit tests for the service layer, but in the end, it was well designed and I succeed in mocking the context and set expectations.
But, I'm starting in with the client side now, and I'm having some problems figuring out how to mock things. It's not so straight forward.
So, I have this sample method that post via HTTP some data using a datacontract and XML as transport.
public class ServiceClient: IService
{
private const string uri_template = "http://{0}:{1}/SomeService.svc/";
private readonly HttpClient http = new HttpClient();
public ServiceClient(string host, int port)
{
http.BaseAddress = new Uri(string.Format(uri_template , host, port));
}
public void Create(MyDataContract my_data_contract)
{
var content = HttpContentExtensions
.CreateDataContract(
my_data_contract,
Encoding.Default,
"text/xml",
null);
var response = http.Post("Create", content);
if (response.StatusCode != HttpStatusCode.Created) {
throw new Exception("something is not right");
}
}
}
This method is working and indeed posting the data and serializing correctly.
What I want to do, is to unit test it.
My questions are:
How do I make stubs/mocks for HttpContextExtensions knowing that it does not implement any interface?
And for HttpClient? this is worst since it is full of extension methods defined elsewhere (Post and the like methods are extensions).
In 2. I guess I can stick to 'Send' and it's overloads, but then is the same problem as in 1
What I was thinking to do, is to extract interfaces for HttpContextExtensions and HttpClient, make a children for each one and implement the interfaces delegating to the parent, and then using the interfaces in my code.
But that's a lot of work IMHO.
I'm using RhinoMocks as my mocking framework so I can't mock/stub concrete classes, and I would like to stick to it, because I really think that mocking concrete classes is not very elegant.
So, is there a cleaner/faster/smarter way to unit test code like the above?
PD: This is for WCF Starter Kit preview 2
If you really want to mock that code, an approach that could work for this scenario is to receive an HttpClient instance in the ServiceClient class. The HttpClient class contains a processing pipeline that you can customize with custom handlers. For your tests, you can basically inject a handler in that httpclient instance to return a mocked response to the test before the real service is called in the last handler (Transport Stage). Take a look at this post to get an idea about how this can be implemented,
http://weblogs.asp.net/cibrax/archive/2009/03/18/negotiating-a-saml-token-for-rest-clients-with-httpclient.aspx
Regards,
Pablo.
I wrote most of the HttpClient code. Pablo's suggestion is what I did in the unit tests -- see FixedTransport.cs in the source zip.