Why is MSpec's ShouldBeOfType<T> assertion extension method missing? - mspec

I'm trying to use Mspec's ShouldBeOfType<T>() assertion extension method, but intellisense says that it can't find it. I'm using MSpec v0.7.0. I tried reinstalling using Nuget but didn't work.
[Subject("Prop Manager")]
public class When_Replying_To_Prop_Which_Already_Had_Emailed_And_No_Overwrite
{
Because of = () => _exception = Catch.Exception(() => _PropManager.ReplyToProp());
It should_result_in_an_error = () => _exception.ShouldBeOfType<InvalidOperationException>();
private static Exception _exception;
}

As of version 0.7.0, Machine.Specifications does not include assertions any more (see Daniel Marbach's blog). You have to install Machine.Specifications.Should (or another assertions library).
Additionally, ShouldBeOfType() has been replaced with ShouldBeOfExactType() (or ShouldBeAssignableTo() respectively), so you should also change that in your code (see github issue.

Related

Polly.Extensions.Http not accessible for IHttpClientFactory - .Net6

I need to use Polly.Extensions.Http for same reason.
I install Polly and Polly.Extensions.Http packages, and have these using's
global using Polly;
global using Polly.Retry;
global using Polly.Timeout;
global using Polly.Extensions.Http;
but when need add to HttpClient, It's not available!
I use these refrence
To summarize the discussion in the comments:
Like Peter Csala said: "The Polly.Extensions.Http package is deprecated. It has not been modified more than 3 years."
The new way to use Polly with the HttpClientFactory is described in the wiki of the Polly github. It seems to use Microsoft.Extensions.Http.Polly.
E.g.
services.AddHttpClient("GitHub", client =>
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
})
.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}));

What is causing the error that swagger is already in the route collection for Web API 2?

I installed Swagger in my ASP.Net MVC Core project and it is documenting my API beautifully.
My co-worker asked me to install it in a full framework 4.6.1 project so I've done the following.
In Package Console Manager run:
Install-Package Swashbuckle
To get your Test Web API controller working:
1) Comment this out in the WebApi.config:
// config.SuppressDefaultHostAuthentication();
// config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Now this URL:
http://localhost:33515/api/Test
brings back XML:
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>value1</string>
<string>value2</string>
</ArrayOfstring>
In Global.asax Register() I call:
SwaggerConfig.Register();
In AppStart.Swagger.Config Register() I put:
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1.0", "HRSA CHAFS");
c.IncludeXmlComments(GetXmlCommentsPath());
})
.EnableSwaggerUi(c =>
{});
}
private static string GetXmlCommentsPath()
{
var path = String.Format(#"{0}bin\Services.XML", AppDomain.CurrentDomain.BaseDirectory);
return path;
}
}
Now I get this error:
"A route named 'swagger_docsswagger/docs/{apiVersion}' is already in the route collection. Route names must be unique."
I've been stuck on this for hours.
How do you get rid of this?
This can happen when you re-name your .NET assembly. A DLL with the previous assembly name will be present in your bin folder. This causes the swagger error.
Delete your bin folder and re-build your solution.
This resolves the swagger error.
Swagger config uses pre-application start hook, so you don't need to call SwaggerConfig.Register() explicitly. Otherwise Register method is called twice.
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
in my case i added another project as refrence and that other project has swagger too.
i remove that refrence and move needed code to new project.
I solved the problem by deleting the SwaggerConfig.cs file from the App_Start folder as I had already created it manually.
Take a look at this link, here also has more useful information:
A route named 'DefaultApi' is already in the route collection error
In my experience the error occurs when you add reference to another project and that project is a service and it occurs on the SwaggerConfig of the referenced project. Removing project reference usually solve the problem, if you need to share classes I suggest you to create a specific project as Class Library and add its reference to both your services

What does compilationOptions.emitEntryPoint mean?

Just installed the rc1 tools and created a new web project to see what has changed in the template.
I noticed that project.json now contains:
"compilationOptions": {
"emitEntryPoint": true
}
But it's unclear what this does.
Does anyone have an idea?
As mentioned below: It looks like it is a flag to the compiler to indicate that the project is a console application vs. a library (namely: a console application must contain public static void Main())
You can see from the source here.
In the new RC1 default web application template, you'll notice at the bottom of Startup.cs there is a new expression bodied method that acts as the entry point:
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
If you remove this method then perform a build (dnu build) you will get an error:
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
However, if you change the emitEntryPoint flag to false and attempt to build again, it will succeed. This is because it is creating a library instead of a console app.
I see this in the source;
var outputKind = compilerOptions.EmitEntryPoint.GetValueOrDefault() ?
OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary;
Looks like it tells the compiler whether to create a Console Application or a Library.
Additionaly, if you create a new Class Library (Package) and Console Application (Package) in VS2015 you'll see that project.json for the Console Application includes the following, while the Class Library does not;
"compilationOptions": {
"emitEntryPoint": true
}

Move to Web API RC, Get: Method not found: 'System.Web.Http.Services.DependencyResolver System.Web.Http.HttpConfiguration.get_ServiceResolver()'

I moved a site from WebAPI Beta to WebAPI RC, and am now getting an error upon loading the site: Method not found: 'System.Web.Http.Services.DependencyResolver System.Web.Http.HttpConfiguration.get_ServiceResolver()'.
The error occurs the first time that I am trying to register my AutoFacWebApiDependencyResolver (per instructions here):
var resolver = new AutofacWebApiDependencyResolver(IoCManager.Container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
The stacktrace shows the following:
[MissingMethodException: Method not found: 'System.Web.Http.Services.DependencyResolver System.Web.Http.HttpConfiguration.get_ServiceResolver()'.]
System.Web.Http.GlobalConfiguration.<.cctor>b__0() +0
System.Lazy`1.CreateValue() +12775823
System.Lazy`1.LazyInitValue() +355
StatusBoard.Web.MvcApplication.RegisterDependencyInjection() in C:\path-tp-app\Global.asax.cs:125
StatusBoard.Web.MvcApplication.Application_Start() in C:\path-to-app\Global.asax.cs:75
Based on this, it appears that the error is occurring during initialization of the static GlobalConfiguration class. When I drill down into the source for that class, I see the following:
private static Lazy<HttpConfiguration> _configuration = new Lazy<HttpConfiguration>((Func<HttpConfiguration>) (() =>
{
local_0 = new HttpConfiguration((HttpRouteCollection) new HostedHttpRouteCollection(RouteTable.Routes));
local_0.get_ServiceResolver().SetService(typeof (IBuildManager), (object) new WebHostBuildManager());
return local_0;
}));
private static Lazy<HttpControllerDispatcher> _dispatcher = new Lazy<HttpControllerDispatcher>((Func<HttpControllerDispatcher>) (() => new HttpControllerDispatcher(GlobalConfiguration._configuration.Value)));
public static HttpConfiguration Configuration
{
get
{
return GlobalConfiguration._configuration.Value;
}
}
The fourth line here seems to be the issue - it is trying to call a get_ServiceResolver() method that no longer exists in the HttpConfiguration class (should be DependncyResolver instead, probably).
Is this just a bug with the RC for WebAPI? Is there some way that I can get around this? Or am I stuck in some DLL/Nuget hell (and if so, how can I extricate myself)?
be sure to include the correct nuget package (RC!) and install the new mvc4rc on the machine where you build your package.
Dependency Resolution has been completely re-written in RC. Best is to uninstall beta DLLs and then your problems most likely go away.
Just uninstall the one flagged as "(bundle)" in windows' "Uninstall a program".

Trying to stub Server.MapPath with MvcContrib Test helpers and Rhino Mocks 3.5

I'm using MvcContrib's test helpers and Rhino Mocks 3.5 to test an ASP.NET MVC action method. I build my fake controller like so:
var _builder = new TestControllerBuilder();
_builder.InitializeController(_controller);
So I get a fake controller that contains fake HTTP Server etc.
I'm then trying to stub the Server.MapPath method like so
controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/APP_DATA/Files/");
but in my method under test the call to Server.MapPath("/APP_DATA/Files/") returns null.
This is the test
const string STOCK_NUMBER_ID = "1";
const string FULL_FILE_PATH = "App-Data/Files";
var controller = CreateStockController();
_uploadedFileTransformer.Stub(x => x.ImageBytes).Return(new byte[10]);
_uploadedFileTransformer.Stub(x => x.ConvertFileToBytes(FULL_FILE_PATH)).Return(true);
controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/App_Data/Files/");
controller.AddImage(Guid.NewGuid(), STOCK_NUMBER_ID);
What I am missing?
Old post but I was searching for this and I found a solution, MvcContrib's TestHelper probably got it fixed because for me it's working.
_builder.HttpContext.Server.Stub(s => s.MapPath("~/" + filepath)).Repeat.Once().Return(mapedPath);
Looks like this is a bug in MVCContrib (at least with what I have on my machine -- v1.0.0.0). When setting up the controller context, it's using Rhino.Mocks record/replay mode, but (and this is the bug), it doesn't put the HttpServer mock into replay mode. It puts everything else in replay mode but not that one.
So a quick fix is to do:
controller.Server.Replay();
As part of your "arrange" section of your test. Then it works fine.