moq

How do I Moq IFindFluent so this call to ToListAsync works?

*爱你&永不变心* 提交于 2019-12-10 12:43:30
问题 I am unit testing a wrapper to the MongoDB C# driver. I have this line of code: Collection.Find(predicate).ToListAsync(); Where Collection is of type IMongoCollection<T> and Find(predicate) returns an instance implementing IFindFluent<T, T> . ToListAsync() is an extension to turn the results into a list, I assume. I am attempting to write unit tests, and I am stumped on handling this. I can't make a wrapper class because that's what I'm working on. I would prefer to either make it so

How to mock a method returning Task<IEnumerable<T>> with Task<List<T>>?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 12:42:53
问题 I'm attempting to set up a unit test initializer (in Moq) where an interface method is being mocked: public interface IRepository { Task<IEnumerable<myCustomObject>> GetSomethingAsync(string someStringParam); } ... [TestInitialize] public void Initialize() { var repoMock = new Mock<IRepository>(); var objectsList = new List<myCustomObject>() { new myCustomObject("Something"), new myCustomObject("Otherthing") } repoMock.Setup<Task<IEnumerable<myCustomObject>>>( rep => rep.GetSomethingAsync(

Mocking a cotroller context asp 4.5

百般思念 提交于 2019-12-10 11:14:58
问题 I have to mock the controller context in order to test some methods that are authorized. The problem is that although I try this it does not work because when I type: var mock = new Mock<ControllerContext>(); Visual Studio underlines ControllerContext and says that The type or namespace could not be found These are the usings i have included in the unit test class: using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Web; using System.Collections.Generic; using Moq;

Entity Framework 6 and Moq4: Is it possible to have a mocked DbSet retain added data for the duration of its scope?

旧时模样 提交于 2019-12-10 10:59:21
问题 Background: - Using EntityFramework 6 - Using Moq v4.2.1402.2112 - Using DbFirst methodology I have been following the EF6 Moq walkthrough (which can be found here) however; I have been wondering if it is possible to have a mocked DbSet retain the data that is added to it for the duration of it's scope? For example: using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using System.Collections.Generic; using System.Data.Entity; using System.Linq; namespace TestingDemo { [TestClass]

Testing a method accepting a delegate with Moq

旧时模样 提交于 2019-12-10 10:35:20
问题 my code is using a component implementing an interface like this public interface IFoo { void DoSomething(string p1); void DoSomething(string p1, Action<string> p2); } As of this moment, I'm using the first method, but I plan to move to the second one and I want to keep my coverage as high as possible. Just that I really don't know how to inspect the delegate or even just setup Moq to mock the interface. I tried with mock.Setup(p => p.DoSomething(It.IsAny<string>(), It.IsAny<Delegate>()));

Moq and reflection, passing dynamically generated expression tree / lambda to moq

独自空忆成欢 提交于 2019-12-10 09:54:46
问题 Is it possible to write code like the following. I'm trying to using Moq with objects that I'm reflecting on as part of a testing framework. The code below raises a "Unhandled expression type: 'Goto'" exception from Moq, which I guess is expecting something different. It kind of looks like it should work though! private void button1_Click(object sender, EventArgs e) { Ifoo = foo Foo(); // Create input parameter for lambda ParameterExpression value = Expression.Parameter(typeof(IFoo), "value")

Moq Roles.AddUserToRole test

点点圈 提交于 2019-12-10 09:43:41
问题 I am writing unit tests for a project in ASP.NET MVC 1.0 using Moq and MvcContrib TestHelper classes. I have run into a problem. When I come to Roles.AddUserToRole in my AccountController, I get a System.NotSupportedException. The Roles class is static and Moq cannot mock a static class. What can I do? 回答1: You could use a pattern like DI (Dependency Injection). In your case, I would pass a RoleProvider to the AccountController, which would be the default RoleProvider by default, and a mock

How can I mock ServiceStack IHttpRequest

大城市里の小女人 提交于 2019-12-10 06:31:37
问题 I'm trying to get a unit test working for a service that is injecting items into the IHttpRequest.Items, using a request filter: this.RequestFilters.Add((req, res, dto) => { // simplified for readability... var repo = container.Resolve<IClientRepository>(); var apiKey = req.Headers["ApiKey"]; // lookup account code from api key var accountcode = repo.GetByApiKey(apiKey); req.Items.Add("AccountCode", accountCode); }); My service uses that dictionary item: public class UserService :

Can mock objects setup to return two desired results?

二次信任 提交于 2019-12-10 05:13:50
问题 Can mock objects be used to return more than one desired result like below? mockObject.Setup(o => o.foo(It.IsAny<List<string>>())).Returns(fooBall); mockObject.Setup(o => o.foo(It.IsAny<int>())).Returns(fooSquare); 回答1: Yes, you can use these setups. Thus arguments of foo method call are different (any integer and any list of strings), you have two different setups here, each with its own return value. If you would have same arguments, then last setup would replace previous setup(s). Remember

Can I access the full power of Autofac in UnitTests, using the Moq integration

女生的网名这么多〃 提交于 2019-12-10 04:26:58
问题 My project (which happens built on top of Orchard, though I don't think that's relevant) uses Autofac. I am writing unit tests in which I want to stub out any dependencies using Moq, and I'm using the Autofac/Moq integration to achieve this. This is fine for any simple dependencies that are being passed in as constructor arguments. (Autofac documentation provides details of how to achieve this here). But because I don't ever have a containerBuilder, I don't see how to use a lot of Autofac's