moq

Testing With A Fake DbContext and Autofixture and Moq

徘徊边缘 提交于 2019-12-22 08:29:26
问题 SO follow this example example and how make a fake DBContex For test my test using just this work fine [Test] public void CiudadIndex() { var ciudades = new FakeDbSet<Ciudad> { new Ciudad {CiudadId = 1, EmpresaId =1, Descripcion ="Santa Cruz", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1}, new Ciudad {CiudadId = 2, EmpresaId =1, Descripcion ="La Paz", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1}, new Ciudad {CiudadId = 3, EmpresaId =1, Descripcion ="Cochabamba

xunit test for IFormFile field in Asp.net Core

风流意气都作罢 提交于 2019-12-22 06:42:13
问题 I have an Asp.net Core method with below definition. [HttpPost] public IActionResult Upload(IFormFile file) { if (file == null || file.Length == 0) throw new Exception("file should not be null"); var originalFileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); file.SaveAs("your_file_full_address"); } I want to create XUnit Test for this function, how could I mock IFormFile ? Update: Controller: [HttpPost] public async Task<ActionResult> Post(IFormFile

How to mock up dbcontext?

依然范特西╮ 提交于 2019-12-22 06:33:02
问题 I am using Entity Framework 7 in .net core 1.0 rc2. Here is the class. public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Blog> Blogs { get; set; } } Then inject the ApplicationDbContext to a class public class BtnValidator { private readonly ApplicationDbContext _dbContext; public BtnValidator(ApplicationDbContext dbContext) { _dbContext = dbContext; } } Not sure how to mock it in unit

Can I use moq's InSequence() with MockBehavior.Loose?

烂漫一生 提交于 2019-12-22 04:09:49
问题 I was trying to perform subsequent calls verification and I found that moq supports the InSequence() method for this, like: MockSequence s = new MockSequence(); validator.InSequence(s).Setup(m => m.IsValid(It.IsAny<Frame>())).Returns(true); encryptor.InSequence(s).Setup(m=>m.Encrypt(It.IsAny<Frame>())); socket.InSequence(s).Setup(m => m.Send(It.IsAny<Frame>())); compressor.InSequence(s).Setup(m => m.Compress(It.IsAny<Frame>())); However, this seems to be working only when I specify mock

Moq property with protected setter

房东的猫 提交于 2019-12-22 03:52:59
问题 I want to Moq next object: abstract class Foo { public string Bar { get; protected set; } } so that new Mock<Foo>().Bar return "Blah" . How can I do that? fooMock.SetupGet<string>(s => s.Bar).Returns("Blah"); throws Failure: System.NotSupportedException : Invalid setup on a non-virtual member: s => s.Date and fooMock.Protected().SetupGet<string>("Bar").Returns("Blah"); throws To specify a setup for public property StatementSection.Date, use the typed overloads 回答1: Since mocking is done by

Moq - Non-overridable members may not be used in setup / verification expressions

左心房为你撑大大i 提交于 2019-12-22 03:52:10
问题 I'm new to Moq. I'm mocking a PagingOptions class. Here is how the class looks like: public class PagingOptions { [Range(1, 99999, ErrorMessage = "Offset must be greater than 0.")] public int? Offset { get; set; } [Range(1, 100, ErrorMessage = "Limit must be greater than 0 and less than 100.")] public int? Limit { get; set; } public PagingOptions Replace(PagingOptions newer) { return new PagingOptions { Offset = newer.Offset ?? Offset, Limit = newer.Limit ?? Limit }; } } Here is my mock

Getting arguments passed to a FakeItEasy-mock without using magic strings?

眉间皱痕 提交于 2019-12-22 03:33:31
问题 I have been using Moq for my mocking needs the last years, but after looking at FakeItEasy i wanted to give it a try. I often want to test that a method have been called with the correct parameters, but i found no satisfactory way to do this with FakeItEasy. I have the following code to test: public class WizardStateEngine : IWizardStateEngine { private readonly IWorkflowInvoker _workflowInvoker; private List<CustomBookmark> _history; public WizardStateEngine(IWorkflowInvoker workflowInvoker)

xUnit and Moq do not support async - await keywords

送分小仙女□ 提交于 2019-12-22 01:27:07
问题 I am trying to discover how to apply the async and await keywords to my xUnit tests. I am using xUnit 1.9 and Async CTP 1.3. Here is my test case I have an interface which specifies one asynchronous method call public interface IDoStuffAsync { Task AnAsyncMethod(string value); } I have a class which consumes the interface and calls the async method public class UseAnAsyncThing { private readonly IDoStuffAsync _doStuffAsync; public UseAnAsyncThing(IDoStuffAsync doStuffAsync) { _doStuffAsync =

Ninject.MockingKernel.Moq security exception

天大地大妈咪最大 提交于 2019-12-22 00:34:10
问题 I am using Ninject for my IoC container and I'm trying to write some unit tests. I found the Ninject Mocking Kernel so I thought I'd give it a go but I can't get the simplest test to pass. I am missing something and need a little help. Moq.4.0.10827.Final Ninject-2.2.0.0-release-net-4.0 Ninject.MockingKernel-2.2.0.0-release-net-4.0 My unit test... [TestMethod] public void Constructor_CanInitialize() { var kernel = new MoqMockingKernel(); var mock = kernel.Get<IDataRepository>(); <--Error here

how do you mock an xml for unit testing?

拟墨画扇 提交于 2019-12-21 12:31:11
问题 I need to unit testing this GetData method. public MessageResponse GetData(XmlElement requestElement) { MessageResponse MsgResponse = new MessageResponse(); if (requestElement.Attributes["employeeNo"] == null){ MsgResponse.Messages = new List<string>(); MsgResponse.Messages.Add("Attribute employeeNo is missing"); MsgResponse.Error = true; return MsgResponse; } if (requestElement.Attributes["xmlEmployeeName"] == null){ MsgResponse.Messages.Add("Attribute xmlEmployeeName is missing");