moq

Moq DbSet NotImplementedException

送分小仙女□ 提交于 2019-12-30 23:46:39
问题 I have a Moq DbSet that has been working until recently, however since the last update of dependencies it keeps throwing a NotImplementedException on IQueryable.Provider Code used as follows: var mockSet = new Mock<DbSet<A>>(); var list = new List<A>(); var queryable = list.AsQueryable(); mockSet.As<IQueryable<A>>().Setup(m => m.Provider).Returns(queryable.Provider); mockSet.As<IQueryable<A>>().Setup(m => m.Expression).Returns(queryable.Expression); mockSet.As<IQueryable<A>>().Setup(m => m

Moq DbSet NotImplementedException

孤人 提交于 2019-12-30 23:46:32
问题 I have a Moq DbSet that has been working until recently, however since the last update of dependencies it keeps throwing a NotImplementedException on IQueryable.Provider Code used as follows: var mockSet = new Mock<DbSet<A>>(); var list = new List<A>(); var queryable = list.AsQueryable(); mockSet.As<IQueryable<A>>().Setup(m => m.Provider).Returns(queryable.Provider); mockSet.As<IQueryable<A>>().Setup(m => m.Expression).Returns(queryable.Expression); mockSet.As<IQueryable<A>>().Setup(m => m

MOQ - verify exception was thrown

╄→尐↘猪︶ㄣ 提交于 2019-12-30 07:51:25
问题 I working with MOQ framework for my testing. I have a scenario in which I expect a fault exception to be thrown. How can I verify it was thrown? public void Koko(List<string?> list) { foreach(string? str in list) { if (str != null) someProperty.Foo(str); else throw new FormatException(); } } Thanks in advance. 回答1: If you want to verify an exception was thrown (by your own code) then Moq is not your tool of choice for that. Simply use one of the unit test frameworks available. Xunit/NUnit:

Unit Test a file upload, how?

試著忘記壹切 提交于 2019-12-30 04:00:15
问题 Using MVC3.NET I have a file upload method in a controller that works fine with the following signature public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file) How can I unit test this with NUnit? I have looked around and everyone seems to point to Moq but I'm new to unit testing and cannot get Moq working. I have found interesting blogs such as this: http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html but am struggling to figure out how the same might be

How to use moq to test a concrete method in an abstract class?

僤鯓⒐⒋嵵緔 提交于 2019-12-30 03:01:27
问题 In the past when I wanted to mock an abstract class I'd simply create a mocked class in code that extended the abstract class, then used that class in my unit testing... public abstract class MyConverter : IValueConverter { public abstract Object Convert(...) { ... }; public virtual Object ConvertBack(...) { ... } } private sealed class MockedConverter : MyConverter { ... } [TestMethod] public void TestMethod1() { var mock = new MockedConverter(); var expected = ...; var actual = mock

How to unit test with ILogger in ASP.NET Core

旧城冷巷雨未停 提交于 2019-12-29 11:43:13
问题 This is my controller: public class BlogController : Controller { private IDAO<Blog> _blogDAO; private readonly ILogger<BlogController> _logger; public BlogController(ILogger<BlogController> logger, IDAO<Blog> blogDAO) { this._blogDAO = blogDAO; this._logger = logger; } public IActionResult Index() { var blogs = this._blogDAO.GetMany(); this._logger.LogInformation("Index page say hello", new object[0]); return View(blogs); } } As you can see I have 2 dependencies, a IDAO and a ILogger And

Experiences using moq with VB.Net

故事扮演 提交于 2019-12-29 08:32:31
问题 I really like the moq mocking framework. I've used it on several projects. Unfortunately, one of my customers is demanding we use VB.Net. Not my preference, but hey, .Net is .Net, right? I've heard that moq has some trouble with VB. Is this true? Is so, what sorts of trouble? I would expect it to work fine given the language agnostic nature of .Net. Should I look into using some other mocking framework for use with VB? 回答1: The main problem of VB.net with regards to mocking frameworks is that

Handling specimen creation inconsistencies between AutoFixture and Moq

♀尐吖头ヾ 提交于 2019-12-29 07:52:30
问题 I am using AutoMoqCustomization in my test conventions. Consider the code below. Everything works great until I add a constructor to one of the concrete classes. When I do, I get "could not find a parameterless constructor". We know AutoFixture doesn't have an issue with the constructor because it delivered me the test object one which proved to be assignable from IThings... no failure there. So it must be moq. This makes some sense because I assume builder was generated by moq and passed

How to moq a NetworkStream in a unit test?

人盡茶涼 提交于 2019-12-29 05:07:24
问题 I'm using Moq & NUnit as a unit test framework. I've written a method that is given a NetworkStream object as a parameter: public static void ReadDataIntoBuffer(NetworkStream networkStream, Queue dataBuffer) { if ((networkStream != null) && (dataBuffer != null)) { while (networkStream.DataAvailable) { byte[] tempBuffer = new byte[512]; // read the data from the network stream into the temporary buffer Int32 numberOfBytesRead = networkStream.Read(tempBuffer, 0, 512); // move all data into the

How to mock WCF client using Moq?

早过忘川 提交于 2019-12-28 16:13:08
问题 In my project I am using: SL5+ MVVM+ Prism + WCF + Rx + Moq + Silverlight Unit Testing Framework. I am new to unit-testing and have recently started into DI, Patterns (MVVM) etc. Hence the following code has a lot of scope for improvement (please fell free to reject the whole approach I am taking if you think so). To access my WCF services, I have created a factory class like below (again, it can be flawed, but please have a look): namespace SomeSolution { public class ServiceClientFactory