Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to mock abstract httpcontext using moq .net core

//Arrange
var request = new Mock<HttpRequest>();
request.Setup(x => x.Scheme).Returns("http");
request.Setup(x => x.Host).Returns(HostString.FromUriComponent("http://localhost:8080"));
request.Setup(x => x.PathBase).Returns(PathString.FromUriComponent("/api"));

var httpContext = Mock.Of<HttpContext>(_ => 
    _.Request == request.Object
);

//Controller needs a controller context 
var controllerContext = new ControllerContext() {
    HttpContext = httpContext,
};
//assign context to controller
var controller = new SomeController(){
    ControllerContext = controllerContext,
};

String expected = "expected value here";

//Act
String actual = await controller.SomeMethodPost();


//Assert
Assert.AreEqual(expected, actual);

//...
Comment

PREVIOUS NEXT
Code Example
Csharp :: DisplayUnitType revit 2022 
Csharp :: ASP.NET Core set update clear cache from IMemoryCache (set by Set method of CacheExtensions class) 
Csharp :: viewresolver cyrillic 
Csharp :: unity Polymorphism 
Csharp :: aws asp.net tutorial 
Csharp :: C# how to stop user type into combobox 
Csharp :: Precision comparison in C# 
Csharp :: hacking 
Csharp :: Delete last modification on EntityFramework Core 
Csharp :: c# psobject get value 
Csharp :: prometheus add prefix to metrics 
Csharp :: unity rotatoin angle 
Csharp :: Conditional IQueryable Linq extension 
Csharp :: screenshot c# WinForms 
Csharp :: how to handle list properties in c# of string type 
Csharp :: c# dubble comment 
Csharp :: c# force arguments to be keywords 
Csharp :: translate nicely between two vector3 
Csharp :: unity inspector sliders 
Csharp :: bitwise and c# 
Csharp :: unity iOS app rating widget 
Csharp :: csharp nullable types 
Csharp :: convert string to int tryparse c# 
Csharp :: Unity inverse kinematics nothing is happening 
Csharp :: weakreference tryget c# 
Html :: html pound symbol 
Html :: how to open link in a new tab 
Html :: tag for tel 
Html :: python find remove html tags 
Html :: regex remove html comment 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =