moq

How to forward to another object when using .NET Moq?

╄→尐↘猪︶ㄣ 提交于 2020-01-24 04:17:10
问题 Given an object, I would like to create a mock that implements the interface of the object and mocks one method, but forwards the rest of the methods to the real object, not the base class . For example: ISqlUtil sqlUtil = GetTheRealSqlUtilObjectSomehow(...); var mock = new Mock<ISqlUtil>(); mock.Setup(o => o.SpecialMethodToBeMocked(...)).Returns<...>(...) // Here I would like to delegate the rest of the methods to the real sqlUtil object. How ? So, in the example I want to mock just ISqlUtil

How to set the value of a query string in test method Moq

对着背影说爱祢 提交于 2020-01-23 05:14:48
问题 I have the following controller action method and I am writing a unit test for this method try { if ( Session["token"] == null) { //checking whether the user has already given the credentials and got redirected by survey monkey by checking the query string 'code' if (Request.QueryString["code"] != null) { string tempAuthCode = Request.QueryString["code"]; Session["token"] = _surveyMonkeyService.GetSurveyMonkeyToken(ApiKey, ClientSecret, tempAuthCode, RedirectUri, ClientId); } else { //User

Mock a method without mocking a class first

六眼飞鱼酱① 提交于 2020-01-23 03:37:26
问题 I am using moq4 for mocking things in my UnitTests . I have a class say TestClass in which a method called TestMethod is their that i wanted to test. So my problem is my TestMethod requires a check on testlist that is in my TestClass. Like this:- public Class TestClass { public readonly ISomeService _someService; public bool TestProperty { get; private set; } public List<string> testlist { get; private set; } Public TestClass(ISomeService someService) { _someService = someService; } public

Mocking methods with Expression<Func<T,bool>> parameter using Moq

落爺英雄遲暮 提交于 2020-01-23 01:19:07
问题 I want to mock this interface using Moq IInterfaceToBeMocked { IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter); } I was thinking of doing something like _mock.Setup(method => method.SearchDudeByFilter( x=> x.DudeId.Equals(10) && X.Ride.Equals("Harley"))). Returns(_dudes);// _dudes is an in-memory list of dudes. When I try to debug the unit test where i need this mocking, it says that "expression is not allowed" pointing towards the lambda. If it makes any difference I

Creating Mock with Moq around existing instance?

狂风中的少年 提交于 2020-01-21 18:57:46
问题 Let's say we have integration test with extensive configuration IConfiguration . I've setup the test to work with autofac containers, and now I'd like to use Mock to replace the operation on one of it's properties without the need to mock or replace everything else: var config = MyTestContainer.Resolve<IConfiguration>(); //let's say that config.UseFeatureX = false; //here, I'd like to create mock "around" the existing instance: var mockedConfig = Mock.CreateWith(config); //CreateWith => a

Creating Mock with Moq around existing instance?

戏子无情 提交于 2020-01-21 18:57:09
问题 Let's say we have integration test with extensive configuration IConfiguration . I've setup the test to work with autofac containers, and now I'd like to use Mock to replace the operation on one of it's properties without the need to mock or replace everything else: var config = MyTestContainer.Resolve<IConfiguration>(); //let's say that config.UseFeatureX = false; //here, I'd like to create mock "around" the existing instance: var mockedConfig = Mock.CreateWith(config); //CreateWith => a

Moq Mocking and tracking Session values

♀尐吖头ヾ 提交于 2020-01-21 07:20:28
问题 I'm having problems returning a Session value set from mocking using Moq. Using the following public class TestHelpers { public long sessionValue = -1; public HttpContextBase FakeHttpContext() { var httpContext = new Mock<HttpContextBase>(); var session = new Mock<HttpSessionStateBase>(); httpContext.Setup(x => x.Session).Returns(session.Object); httpContext.SetupGet(x => x.Session["id"]).Returns(sessionValue); httpContext.SetupSet(x => x.Session["id"] = It.IsAny<long>()) .Callback((string

NUnit/Moq: I have mocked a class but real contructor is executed, why?

橙三吉。 提交于 2020-01-16 01:53:49
问题 I have a large legacy WPF project that I'm now trying to get unit tested with NUnit (v. 2.6.3) and Moq (v. 4.2), but I'm having trouble with mocking certain classes. There's one in particular, a control class derived from System.Windows.Forms.Integration.WindowsFormsHost , that's needed all over the project and has a lot of external dependencies, so it's very important to be able to mock it. Let's call that class Foo , and here's the test case: [TestFixture,RequiresSTA] public class

System.Collections.Generic.KeyNotFoundException with Mono, but not Microsoft's .NET

[亡魂溺海] 提交于 2020-01-16 00:57:45
问题 I found a really strange problem while creating unit tests that only occurs with the Mono runtime (Xamarin on Mac included), but runs fine within Visual Studio. I isolated it as far as I could, and I reached a point that I can't tell if it is a bug with Mono, Moq or Castle DinamicProxy, although it only crashes when using the Mono runtime. This is the code: using System; using System.Collections.Generic; using Moq; namespace ConsoleApplication1 { public interface ISomething<T> { List<T>

Moq - How to unit test changes on a reference in a method

柔情痞子 提交于 2020-01-15 07:27:49
问题 Another day , another question. My service layer has the following method public MatchViewData CreateMatch(string user) { var matchViewData = !HasReachedMaxNumberOfMatchesLimit(user) ? CreateMatchAndAddToRepository(user) : MatchViewData.NewInstance(new Match(user)); matchViewData.LimitReached = HasReachedMaxNumberOfMatchesLimit(user); return matchViewData; } The method calls the this helper method to create a new match object: private MatchViewData CreateMatchAndAddToRepository(string user) {