moq

Moq - verifying a call with parameter that is changed during the execution of the test

人盡茶涼 提交于 2020-01-03 17:32:41
问题 Given the following code public class Entity { public string Name { get; set; } public string Status { get; set; } } public interface IRepository { void InsertEntity(Entity entity); void UpdateEntity(Entity entity); } public class Processor { private IRepository _repository; public Processor(IRepository repository) { _repository = repository; } public void Execute(string name) { var entity = new Entity() { Name = name, Status = "Initialized" }; _repository.InsertEntity(entity); // do other

Moq - verifying a call with parameter that is changed during the execution of the test

两盒软妹~` 提交于 2020-01-03 17:31:06
问题 Given the following code public class Entity { public string Name { get; set; } public string Status { get; set; } } public interface IRepository { void InsertEntity(Entity entity); void UpdateEntity(Entity entity); } public class Processor { private IRepository _repository; public Processor(IRepository repository) { _repository = repository; } public void Execute(string name) { var entity = new Entity() { Name = name, Status = "Initialized" }; _repository.InsertEntity(entity); // do other

Why is Controller.Url null when I unit test my action?

眉间皱痕 提交于 2020-01-03 15:32:10
问题 I've followed this answer to mock HttpContext, Request, Response and User and set Controller.ControllerContext and Controller.Url . Controller.Url is most definitely not null before calling controller.Index() . However, inside controller.Index() , it is null. It seems very strange. What am I missing? Here's my test fixture: [TestFixture] public class ControllerFixtureBase { private Mock<HttpContextBase> _httpContextMock; private RouteCollection _routes; [SetUp] public void SetUp() { var

NullReferenceException thrown when testing custom AuthorizationAttribute

☆樱花仙子☆ 提交于 2020-01-03 11:12:27
问题 I have taken a look at: How do I make a unit test to test a method that checks request headers? How to mock Controller.User using moq How do I unit test a controller method that has the [Authorize] attribute applied? I am trying to test a custom AuthorizeAttribute that I wrote. I have tried many different things to get it to work. This is my current attempt. [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)] public class ConfigurableAuthorizeAttribute :

How to Mock ILogger / ILoggerService using Moq

这一生的挚爱 提交于 2020-01-03 08:22:21
问题 I'm writing some unit tests for my View Model class. The constructor of this class is injected with an ILoggerService. This interface defines 1 method GetLog which returns an ILogger. Something like below where this represents a class that implements ILoggable:- protected ViewModelBase(ILoggerService loggerService) { Logger = loggerService.GetLog(this); } I'm trying to unit test my CreateNewOrder method that looks like below: private void CreateNewOrder(INewOrderViewModel newOrderViewModel) {

How to do Setup of mocks with Ninject's MockingKernel (moq)

五迷三道 提交于 2020-01-03 07:38:09
问题 I'm having a really hard time trying to figure how I can do .SetupXXX() calls on the underlying Mock<T> that has been generated inside the MockingKernel . Anyone who can shed some light on how it is supposed to work? 回答1: You need to call the GetMock<T> method on the MoqMockingKernel which will return the generated Mock<T> on which you can call your .SetupXXX()/VerifyXXX() methods. Here is an example unit test which demonstrates the GetMock<T> usage: [Test] public void Test() { var

Unit Testing ASP.NET Web API

瘦欲@ 提交于 2020-01-03 05:08:08
问题 I'm unit testing a simple post: public HttpResponseMessage<Document> PostDocument(Document document) { document = repository.Add(document); var response = new HttpResponseMessage<Document>(document, HttpStatusCode.Created); var uri = Url.Route(null, new { id = document.Id }); response.Headers.Location = new Uri(Request.RequestUri, uri); return response; } However, the 'URL' and 'Request' are obviously going to be null. Is there an alternative to mocking out ControllerContext and HttpContext?

Mock Entity Framework long LINQ query

拜拜、爱过 提交于 2020-01-02 14:44:37
问题 Have a LINQ query expression (below) that want to test mocking the Entity Framework. To mock I am using the code: Entity Framework Testing with a Mocking Framework This works for other queries but doesn't work for this var query = from vwPs in _reportingDbContext.VwAccountNumber join ps in _reportingDbContext.PaymentSummary on new { Key1 = vwPs.Id, Key2 = vwPs.AccountNumber, Key3 = true, Key4 = true } equals new { Key1 = ps.Id, Key2 = ps.AccountNumber, Key3 = ps.PaymentDate >= fromDateTime,

Mock Entity Framework long LINQ query

☆樱花仙子☆ 提交于 2020-01-02 14:44:26
问题 Have a LINQ query expression (below) that want to test mocking the Entity Framework. To mock I am using the code: Entity Framework Testing with a Mocking Framework This works for other queries but doesn't work for this var query = from vwPs in _reportingDbContext.VwAccountNumber join ps in _reportingDbContext.PaymentSummary on new { Key1 = vwPs.Id, Key2 = vwPs.AccountNumber, Key3 = true, Key4 = true } equals new { Key1 = ps.Id, Key2 = ps.AccountNumber, Key3 = ps.PaymentDate >= fromDateTime,

how to moq simple add function that uses Unit of Work and Repository Pattern

随声附和 提交于 2020-01-02 12:55:06
问题 My test looks like this [Fact] public void SimpleAddTest() { // Arrange var authorizationsToBeAdded = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo (), new PatientPayerAuthInfo () }.ToList(); var persistentAuthorizations = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo {PatientPayerAuthInfoId = 1 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 2 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 3 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 4 } }