moq

Moq Params TargetParameterCountException : Parameter count mismatch Exception

青春壹個敷衍的年華 提交于 2019-12-28 15:12:20
问题 Following is my generic base repository interface public interface IRepository<T> { IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties); } my entity public class Sdk { public Sdk() { this.Identifier = Guid.NewGuid().ToString(); } public virtual ICollection<Resource> AccessibleResources { get; set; } public string Identifier { get; set; } } and following is the specific repo public interface ISdkRepository : IRepository<Sdk> { } now I am trying to test a

Settings variable values in a Moq Callback() call

本小妞迷上赌 提交于 2019-12-28 11:41:44
问题 I think I may be a bit confused on the syntax of the Moq Callback methods. When I try to do something like this: IFilter filter = new Filter(); List<IFoo> objects = new List<IFoo> { new Foo(), new Foo() }; IQueryable myFilteredFoos = null; mockObject.Setup(m => m.GetByFilter(It.IsAny<IFilter>())) .Callback( (IFilter filter) => myFilteredFoos = filter.FilterCollection(objects)) .Returns(myFilteredFoos.Cast<IFooBar>()); This throws a exception because myFilteredFoos is null during the Cast

Moq'ing methods where Expression<Func<T, bool>> are passed in as parameters

◇◆丶佛笑我妖孽 提交于 2019-12-28 04:52:45
问题 I'm very new to unit testing and mocking! I'm trying to write some unit tests that covers some code that interacts with a data store. Data access is encapsulated by IRepository: interface IRepository<T> { .... IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate); .... } The code that I'm trying to test, utilising a concrete IoC'd implementation of IRepository looks like this: public class SignupLogic { private Repository<Company> repo = new Repository<Company>(); public void

Setup Moq To Return Multiple Values

本秂侑毒 提交于 2019-12-25 18:15:25
问题 I am new to Moq and I want to have a test like this: [Fact] public void IsClientExternalForWebShouldReturnFalse() { // Arrange var request = new Mock<HttpRequestBase>(); request.Setup(r => r.UserHostAddress).Returns(new Queue<string>(new[] { "127.0.0.1", "10.1.10.1" }).Dequeue); var context = new Mock<HttpContextBase>(); context.SetupGet(c => c.Request).Returns(request.Object); var service = new EnvironmentService(context.Object, null); // Act / Assert service.IsClientExternal.Should()

Mocking DbContext in Entity Framework 6.1

早过忘川 提交于 2019-12-25 09:24:24
问题 I have found a number of examples that show (apparently) a clear working example of mocking DbContext with EF 6, however, none of them seem to work for me and I am not entirely sure why. This is my unit test code that sets up the mock; var mockData = new List<User> { new User { Email = "my@email.com", Id = 1 } }.AsQueryable(); var mockSet = new Mock<DbSet<User>>(); mockSet.As<IQueryable<User>>().Setup(m => m.Provider).Returns(mockData.Provider); mockSet.As<IQueryable<User>>().Setup(m => m

How to unit test a method with HttpWebRequest/Response dependencies

£可爱£侵袭症+ 提交于 2019-12-25 07:05:44
问题 Been trying to unit test this method but can't figure out how to do it. public bool ValidateCaptcha(string captchaResponse, string captchaChallenge, string hostip) { var strPrivateKey = _secConfiguration.CaptchaPrivateKey; var strParameters = "verify?privatekey=" + strPrivateKey + "&remoteip=" + hostip + "&challenge=" + captchaChallenge+ "&response=" + captchaResponse; var url = CaptchaUrl + strParameters; var request = CreateHttpWebRequest(url); request.Proxy.Credentials = CredentialCache

Moq Unit Testing of Linq Find results in NullReferenceException

心已入冬 提交于 2019-12-25 06:24:09
问题 I have recently started working on Moq for asp.net mvc . I get a NullReferenceException when the Find statement is executed. The code snippet is below. There is a conflict when I try to unit test a Repository class method that has both linq Find and Linq Query statement. I believe that the last statement for dbContext.Setup in case of Students gets prioritized first. If dbContext.Setup for Find is placed after dbContext.Setup for Studnets, then Find works but it fails in Linq query for

MVVM unit tests - message bus only works once

白昼怎懂夜的黑 提交于 2019-12-25 05:08:37
问题 I am trying to write some unit tests against a view model using SimpleMvvmToolkit, and several of these tests require a message be sent to the message bus to fire events inside the view model. The problem is it appears that I can only 'use' the message bus once; the first test that sends a message passes, but the other two that send a message fail. But if I run each test individually, all three pass, and if I change the order, the first test, no matter which one it is, passes. Here is a

MS Fakes Static Method that Returns Class with Private Constructor

岁酱吖の 提交于 2019-12-25 04:22:52
问题 I'm trying to fake/stub out System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name I'd like to know how to assign GetComputerDomain to return a Domain with a Name of "TestDomain". I can return a null domain as follows: System.DirectoryServices.ActiveDirectory.Fakes.ShimDomain .GetComputerDomain = () => { return null; }; But I think the main issue is that the Domain class does not have a public constructor so I can't do the following: System.DirectoryServices.ActiveDirectory

Using moq on an override of abstract method?

时光毁灭记忆、已成空白 提交于 2019-12-25 03:52:51
问题 I have this base class public abstract class Third : IThird { public abstract ThirdUser GetUserDetails(HttpRequestBase request); } and this derived class public class LiProvider : Third { public override ThirdUser GetUserDetails(HttpRequestBase request) { } } I tried to Moq this override like so: mockLiProvider.Setup(x => x.GetUserDetails(It.IsAny<HttpRequestWrapper>())).Returns(user); but it returns null , not the user in the Setup . user is definitely initialised in this test. How can I