moq

Async methods return null

百般思念 提交于 2019-11-30 11:28:20
问题 If I try to mock a type containing an async method such as : interface Foo { Task<int> Bar(); } Then the mock's Bar method is returning null. I guess Moq is choosing default(Task<int>) as default return value for my method, which is indeed null . However Moq should rather choose something like Task.FromResult(default(int)) as default value. Can I force Moq to make async methods returning non-null Tasks ? 回答1: If someone is interested, I made an extension class which makes async methods

Moq to set up a function return based on called times

丶灬走出姿态 提交于 2019-11-30 11:05:10
I need to mock an interface to call to MSMQ, is there a way I can use Moq to simulate real MSMQ scenario that there are 10 messages in the queue, I call mocked function 10 times and I can get a pre-defined object, on 11th time I should get a different return value (e.g. null)? TGH Moq now has an extension method called SetupSequence() in the Moq namespace which means you can define a distinct return value for each specific call. The general idea is that that you just chain the return values you need. In the example bellow the first call will return Joe and the second call will return Jane :

Create an Expression<Func<,>> using reflection

℡╲_俬逩灬. 提交于 2019-11-30 10:59:45
问题 Im using Moq to create mocks of a data set. I have created a little helper class that allows me to have an in memory storage instead of a database that makes unit testing a breeze. That way I can add and remove items from my mock data set, this allows me to test my insert and delete service calls. During the setup of the mock I have a line that looks like the following this.Setup(i => i.AcademicCycles).Returns(mockStore.GetList<AcademicCycle>()); My mock has a lot of properties so I would

Moq ReturnsAsync() with parameters

[亡魂溺海] 提交于 2019-11-30 10:44:43
I'm trying to mock a repository's method like that public async Task<WhitelistItem> GetByTypeValue(WhitelistType type, string value) using Moq ReturnsAsync, like this: static List<WhitelistItem> whitelist = new List<WhitelistItem>(); var whitelistRepositoryMock = new Mock<IWhitelistRepository>(); whitelistRepositoryMock.Setup(w => w.GetByTypeValue(It.IsAny<WhitelistType>(), It.IsAny<string>())) .ReturnsAsync((WhitelistType type, string value) => { return (from item in whitelist where item.Type == type && item.Value == value select item).FirstOrDefault(); }); but i'm getting this error in the

What is the purpose of VerifyAll() in Moq?

半城伤御伤魂 提交于 2019-11-30 10:43:38
I read the question at What is the purpose of Verifiable() in Moq? and have this question in my mind. Need your help to explain that. ema VerifyAll() is for verifying that all the expectations have been met. Suppose you have: myMock.Setup(m => m.DoSomething()).Returns(1); mySut.Do(); myMock.VerifyAll(); // Fail if DoSomething was not called HTH I will try to complete @ema's answer, probably it will give more insights to the readers. Imagine you have mocked object, which is a dependency to your sut . Let's say it has two methods and you want to set them up in order to not get any exceptions or

Mocking virtual readonly properties with moq

ぐ巨炮叔叔 提交于 2019-11-30 10:43:30
I couldn't find a way to do this, though this can be done by hand so why not with moq? Given this class public abstract class MyAbstraction { public virtual string Foo { get { return "foo"; } } } you can set up Foo (a read-only property) like this: var stub = new Mock<MyAbstraction>(); stub.SetupGet(x => x.Foo).Returns("bar"); stub.Object.Foo will now return "bar" instead of "foo". 来源: https://stackoverflow.com/questions/1454186/mocking-virtual-readonly-properties-with-moq

How to Properly Test Controllers in ASP.net MVC that has database calls

巧了我就是萌 提交于 2019-11-30 09:48:14
I am working on an ASP.net MVC 3.0 Application. I am using MSTest along with Moq for unit testing. I have written all the test methods for my controllers and ran those tests , which gave successful results. Now, I have a doubt whether I have properly made unit testing. Because, almost most of my controller actions contains database calls. I am not mocking them , I am mocking only Session and Request objects using Moq. Is it really necessary to mock database calls, since unit testing means testing a single unit of code? I think unit testing controller with database calls violates above

How to mock Entity Framework 6 Async methods?

岁酱吖の 提交于 2019-11-30 09:30:34
I am new in mocking. I want to mock up my base repository which is depend on Entity Framework 6 DbContext But I fail. I searched in Google a lot but did not get any sufficient result. At last I got an example at testing with async queries and try to follow but it is worked for me. Here is my code : DbContext : public class TimeSketchContext : DbContext { public virtual DbSet<EmployeeSkill> EmployeeSkill { get; set; } } Base Repository : public class BaseRepository<T> : IRepositoryBase<T> where T : class, IEntity, new() { protected readonly DbContext InnerDbContext; protected DbSet<T>

Unit testing an HttpApplication

时光毁灭记忆、已成空白 提交于 2019-11-30 08:48:43
I have a class derived from HttpApplication that adds some extra features. I'm to the point where I need to unit test these features, which means I have to be able to create a new instance of the HttpApplication, fake a request, and retrieve the response object. How exactly do I go about unit testing an HttpApplication object? I'm using Moq at the moment, but I have no idea how to set up the required mock object. Unfortunately, this isn't particularly easy to do, as the HttpApplication doesn't lend itself to mocking very easily; there is no interface to mock against and most of the methods

Setup() vs SetupGet()

▼魔方 西西 提交于 2019-11-30 08:03:45
What is the difference between the SetupGet() and Setup() methods for MOQ? Setup() can be used for mocking a method or a property. SetupGet() is specifically for mocking the getter of a property. Took a quick peek at the Moq source code and it looks like if you use Setup() on a property getter, it will call SetupGet(). So in that case, it is probably more personal preference as to whether you want to be more explicit and use SetupGet() instead of Setup(). Of course, my knowledge of Moq is limited, so I don't know if there special cases where you would need to use SetupGet() over Setup().