moq

Mock presence of Authorize attribute

你说的曾经没有我的故事 提交于 2019-12-02 12:13:18
问题 I was wondering how you could mock that a controller is decorated with the [Authorize] attribute in MVC3? I am using a custom membership provider. I would like to test that a controller been decorated with the attribute and you are authorized and what happens when you are not. I'm using Moq. Any good suggestions on this?! UPDATE: I'm currently getting an NullreferenceException saying "object reference not set to an instance of an object". It's the same error as previously mentioned in this

How to Mock/Stub or simply ignore HttpRequest when unit testing [duplicate]

一世执手 提交于 2019-12-02 11:29:03
问题 This question already has answers here : How to mock the Request on Controller in ASP.Net MVC? (8 answers) Closed 4 years ago . public class DemoController : Controller { private readonly ICommonOperationsRepository _commonRepo; public DemoController (ICommonOperationsRepository commonRepo) { _commonRepo = commonRepo; } public ActionResult Default() { var model = new DemoModel(); try { **DeviceDetection dd = new DeviceDetection(Request.ServerVariables["HTTP_X_REWRITE_URL"].ToString()); dd

How to UnitTest a Function in a mocked method

随声附和 提交于 2019-12-02 08:42:28
How can I test the DeleteAppointmentById here? Func<IDataAdapterRW, IEnumerable<uint>> function = db => DeleteAppointmentById(db, appointmentId); return _dataContextProvider.GetContextRW().Run(function); _dataContextProvider is mocked with moq. If I run the test it never enters DeleteAppointmentById of course The method to test: public IEnumerable<uint> DeleteAppointment(uint appointmentId) { Func<IDataAdapterRW, IEnumerable<uint>> function = db => DeleteAppointmentById(db, appointmentId); return _dataContextProvider.GetContextRW().Run(function); } DeleteAppointmentById is the inner method

How do you mock adding items to a repository or DbContext using moq?

一个人想着一个人 提交于 2019-12-02 08:33:40
问题 The examples I've seen for using moq for a repository only show how to mock things being returned. I have a somewhat strange requirement: when a query is executed, if a condition exists, a certain item should be added to the repository. I am wondering how to test this without querying the database. I know how to mock the condition existing, but then how do you setup the mock so that you can test that the certain item is added? 回答1: Try to use fake in memory repository instead of moq, for

How to Mock/Stub or simply ignore HttpRequest when unit testing [duplicate]

孤人 提交于 2019-12-02 06:36:17
This question already has an answer here: How to mock the Request on Controller in ASP.Net MVC? 8 answers public class DemoController : Controller { private readonly ICommonOperationsRepository _commonRepo; public DemoController (ICommonOperationsRepository commonRepo) { _commonRepo = commonRepo; } public ActionResult Default() { var model = new DemoModel(); try { **DeviceDetection dd = new DeviceDetection(Request.ServerVariables["HTTP_X_REWRITE_URL"].ToString()); dd.DetectDevice();** model.ListTopListing.AddRange(_commonRepo.GetListings()); } catch (Exception ex) { ExceptionHandler objErr =

Mock presence of Authorize attribute

穿精又带淫゛_ 提交于 2019-12-02 06:11:19
I was wondering how you could mock that a controller is decorated with the [Authorize] attribute in MVC3? I am using a custom membership provider. I would like to test that a controller been decorated with the attribute and you are authorized and what happens when you are not. I'm using Moq. Any good suggestions on this?! UPDATE: I'm currently getting an NullreferenceException saying "object reference not set to an instance of an object". It's the same error as previously mentioned in this post NullReferenceException while using Authorize Attribute This is related to the Authorize attribute. I

Moq Returns with multiple Linq Expressions

别等时光非礼了梦想. 提交于 2019-12-02 05:38:15
I have the following method within a repository that I am trying to Mock: IEnumerable<TEntity> GetAll( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") I've setup the following: mockContactNumberRepository.Setup(x => x.GetAll( It.IsAny<Expression<Func<ContactNumber, bool>>>(), It.IsAny<Func<IQueryable<ContactNumber>, IOrderedQueryable<ContactNumber>>>(), It.IsAny<string>())) .Returns(new Func<Expression<Func<ContactNumber, bool>>, IQueryable<ContactNumber>>(ex => _contactNumbers.Where(ex.Compile(

.Net core testing with Xunit

懵懂的女人 提交于 2019-12-02 04:44:51
问题 This is the first time I am writing test case and I am not sort of stuck and not sure how to proceed further. I have the following API. In the below sample I have 2 endpoints which I want to perform testing. public class ValuesController : Controller { //This interface is used to setup dynamo db and connection to aws private IDynamoDbClientInitialization _clientAccessor; private static string dynamoDbTable = string.Empty; public ValuesController(IOptions<Dictionary<string, string>>

ASP.Net Core unit testing async controller [duplicate]

99封情书 提交于 2019-12-02 02:13:57
问题 This question already has answers here : How to mock an async repository with Entity Framework Core (4 answers) Closed 4 months ago . I have this test: [Fact] public async void Can_Paginate() { //fake data var product1 = new Product { ProductId = 1, ProductName = "P1" }; var product2 = new Product { ProductId = 2, ProductName = "Product 2" }; var product3 = new Product { ProductId = 3, ProductName = "Product 3" }; var product4 = new Product { ProductId = 4, ProductName = "Product 4" }; var

.Net core testing with Xunit

▼魔方 西西 提交于 2019-12-02 01:41:48
This is the first time I am writing test case and I am not sort of stuck and not sure how to proceed further. I have the following API. In the below sample I have 2 endpoints which I want to perform testing. public class ValuesController : Controller { //This interface is used to setup dynamo db and connection to aws private IDynamoDbClientInitialization _clientAccessor; private static string dynamoDbTable = string.Empty; public ValuesController(IOptions<Dictionary<string, string>> appSettings, IDynamoDbClientInitialization clientAccessor) { var vals = appSettings.Value; dynamoDbTable = vals[