moq

Unit Testing with IMongoQueryable

陌路散爱 提交于 2020-01-15 07:13:07
问题 I am using .NET Core 2.0 and the .NET Core MongoDB driver. I have created a repository like so: public interface IRepository<T> { IMongoQueryable<T> Get() } I have done this to give flexibility to whoever uses this to be able to do LINQ much like they would do using EF. The problem is when it comes to unit testing and I'm trying to create an in-memory database so I can check states before and after operation. Some stuff I tried: public class InMemoryRepository : IRepository<ConcreteType> {

Mock Static class using moq

走远了吗. 提交于 2020-01-13 09:22:11
问题 I am writing unit test cases with the help of NUnit and have some static classes that I need to mock to run test cases so can we mock static class with the help of MOQ mocking framework? Please suggest If some have idea. 回答1: There are two ways to accomplish this - As PSGuy said you can create an Interface that your code can rely on, then implement a concrete that simply calls the static method or any other logging implementation like NLog. This is the ideal choice. In addition to this if you

Inspect DefaultHttpContext body in unit test situation

非 Y 不嫁゛ 提交于 2020-01-13 08:39:08
问题 I'm trying to use the DefaultHttpContext object to unit test my exception handling middleware. My test method looks like this: [Fact] public async Task Invoke_ProductionNonSuredException_ReturnsProductionRequestError() { var logger = new Mock<ILogger<ExceptionHandlerMiddleware>>(); var middleWare = new ExceptionHandlerMiddleware(next: async (innerHttpContext) => { await Task.Run(() => { throw new Exception(); }); }, logger: logger.Object); var mockEnv = new Mock<IHostingEnvironment>();

Simulate a delay in execution in Unit Test using Moq

╄→гoц情女王★ 提交于 2020-01-12 14:03:44
问题 I'm trying to test the following: protected IHealthStatus VerifyMessage(ISubscriber destination) { var status = new HeartBeatStatus(); var task = new Task<CheckResult>(() => { Console.WriteLine("VerifyMessage(Start): {0} - {1}", DateTime.Now, WarningTimeout); Thread.Sleep(WarningTimeout - 500); Console.WriteLine("VerifyMessage(Success): {0}", DateTime.Now); if (CheckMessages(destination)) { return CheckResult.Success; } Console.WriteLine("VerifyMessage(Pre-Warning): {0} - {1}", DateTime.Now,

Unit test Entity Framework using moq

巧了我就是萌 提交于 2020-01-12 03:26:18
问题 I'm using entity framework and trying to unit test my data services which are using EF. I'm not using repository and unit of work patterns. I tried the following approach to mock the context and DbSet: private static Mock<IEFModel> context; private static Mock<IDbSet<CountryCode>> idbSet; [ClassInitialize] public static void Initialize(TestContext testContext) { context = new Mock<IEFModel>(); idbSet = new Mock<IDbSet<CountryCode>>(); context.Setup(c => c.CountryCodes).Returns(idbSet.Object);

Using Moq, how do I set up a method call with an input parameter as an object with expected property values?

╄→尐↘猪︶ㄣ 提交于 2020-01-11 17:09:29
问题 var storageManager = new Mock<IStorageManager>(); storageManager.Setup(e => e.Add(It.IsAny<UserMetaData>())); The Add() method expects a UserMetaData object which has a FirstName property. I'd like to make sure that an object of type UserMetaData with the FirstName of "FirstName1" has been passed. 回答1: You can use Verify . Examples: Verify that Add was never called with an UserMetaData with FirstName != "FirstName1" : storageManager.Verify(e => e.Add(It.Is<UserMetaData>(d => d.FirstName!=

Verify a method call using Moq

心不动则不痛 提交于 2020-01-11 15:05:25
问题 I am fairly new to unit testing in C# and learning to use Moq. Below is the class that I am trying to test. class MyClass { SomeClass someClass; public MyClass(SomeClass someClass) { this.someClass = someClass; } public void MyMethod(string method) { method = "test" someClass.DoSomething(method); } } class Someclass { public DoSomething(string method) { // do something... } } Below is my TestClass: class MyClassTest { [TestMethod()] public void MyMethodTest() { string action="test"; Mock

How can I convert a Predicate<T> to an Expression<Predicate<T>> to use with Moq?

旧时模样 提交于 2020-01-11 08:41:07
问题 Please help this Linq newbie! I'm creating a list inside my class under test, and I would like to use Moq to check the results. I can easily put together a Predicate which checks the results of the list. How do I then make that Predicate into an Expression? var myList = new List<int> {1, 2, 3}; Predicate<List<int>> myPredicate = (list) => { return list.Count == 3; // amongst other stuff }; // ... do my stuff myMock.Verify(m => m.DidStuffWith(It.Is<List<int>>( ??? ))); ??? needs to be an

How to mock the controller context with moq

冷暖自知 提交于 2020-01-11 08:37:19
问题 I am trying out the MOQ framework and up now I have hit a barrier. The following unit test fails because the actual value of the ViewName property is an empty string. Could anyone point me in the right direction please as to why this is not passing the test? [TestMethod] public void Can_Navigate_To_About_Page() { var request = new Mock<HttpRequestBase>(); request.Setup(r => r.HttpMethod).Returns("GET"); var mockHttpContext = new Mock<HttpContextBase>(); mockHttpContext.Setup(c => c.Request)

Moq an indexed property and use the index value in the return/callback

泄露秘密 提交于 2020-01-11 08:24:45
问题 I want to moq a property that has an index, and I want to be able to use the index values in the callback, the same way you can use method arguments in the callback for moq'd methods. Probably easiest to demonstrate with an example: public interface IToMoq { int Add(int x, int y); int this[int x] { get; set; } } Action<int, int> DoSet = (int x, int y) => { Console.WriteLine("setting this[{0}] = {1}", x, y); throw new Exception("Do I ever get called?"); }; var mock = new Mock<IToMoq>