moq

Mocking IDocumentQuery in Unit Test that uses Linq queries

百般思念 提交于 2019-11-28 11:28:57
I am writing unit tests for DocumentDBRepository but I got a null reference exception. I use Moq framework and XUnit. Here's my methods in DocumentDBRepository class. public class DocumentDBRepository<T> : IRepository<T> where T: class { private static string DatabaseId; private static string CollectionId; private static IDocumentClient client; public DocumentDBRepository(IDocumentClient documentClient, string databaseId, string collectionId) { DatabaseId = databaseId; CollectionId = collectionId; client = documentClient; CreateDatabaseIfNotExistsAsync().Wait();

How to test a method in an abstract class with abstract methods?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 10:58:10
问题 It is necessary to check implementation of 'MyMethod' virtual method in the abstract 'MyAbstractClass': public abstract MyAbstractClass { public void MyMethod() { Testpassed = true; } public abstract int StatusCode{get;internal set;} // **EDIT**: internal setter was added public bool TestPassed{get;private set;} } I've tried to do the following: [TestMethod()] { Mock<MyAbstractClass> mockClass = new Mock<MyAbstractClass>(); mockClass.Object.MyMethod(); Assert.IsTrue(mockClass.Object

How to mock WCF client using Moq?

纵然是瞬间 提交于 2019-11-28 10:31:22
In my project I am using: SL5+ MVVM+ Prism + WCF + Rx + Moq + Silverlight Unit Testing Framework. I am new to unit-testing and have recently started into DI, Patterns (MVVM) etc. Hence the following code has a lot of scope for improvement (please fell free to reject the whole approach I am taking if you think so). To access my WCF services, I have created a factory class like below (again, it can be flawed, but please have a look): namespace SomeSolution { public class ServiceClientFactory:IServiceClientFactory { public CourseServiceClient GetCourseServiceClient() { var client = new

Moq Params TargetParameterCountException : Parameter count mismatch Exception

和自甴很熟 提交于 2019-11-28 10:00:41
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 controller, using moq Following is the code I am trying to test public ActionResult GetResources(string

Setup on Mock not returning expected value

ぐ巨炮叔叔 提交于 2019-11-28 09:27:10
问题 Here is a simplified version of a problem I encountered: public interface IService { IProvider Provider { get; } } public interface IProvider { List<int> Numbers{ get; } string Text { get; } } [TestMethod] public void ServiceTest() { var service = new Mock<IService>(); var provider = new Mock<IProvider>(); service.Setup(s => s.Provider).Returns(provider.Object); // A service.Setup(s => s.Provider.Text).Returns("some text"); // B - incorrect // they actually meant to do this, instead of 'B' //

How to mock Session Object in asp net core

元气小坏坏 提交于 2019-11-28 09:18:50
问题 I am writing unit tests using moq framework. I am calling one method in base controller setsession() which will set session using SetString("userdata",object) and I have one more method getsession() which will get session. var sessionMock = new Mock<ISession>(); sessionMock.Setup(s => s.GetString("userdata")).Returns(Object);--failing sessionMock.Setup(s => s.SetString("userdata",object));--failing I am getting the error in s.GetString and s.SetString . As GetString and SetString are

Mocking internal classes with Moq for unit testing

别来无恙 提交于 2019-11-28 09:00:37
Say I have a class "ClassA", which has a dependency on a class "ClassB" (injected into the constructor of ClassA). I want to mock ClassB so that I can test ClassA in isolation. Both classes are internal. Correct me if I'm wrong but it looks like Moq can only mock a class if it is public, it has a public parameterless constructor, and the methods to be mocked are public virtual . I don't really want to make these classes publicly visible. Am I missing something with Moq, or is it just not suitable for what I want to do? I guess I could create an interface (say "IClassB") that ClassB implements,

Moq a function with anonymous type

ぃ、小莉子 提交于 2019-11-28 08:36:57
问题 I'm trying to mock this method Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc) like this iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, object>>())).ReturnsAsync(new { isPair = false }); The method to test doing the call passing an anonymous type to the generic parameter like this instance.GetResultAsync(u => new {isPair = u == "something" }) //dont look at the function return because as generic could have diferent implementations in many case Moq never

MVC3 unit testing response code

孤人 提交于 2019-11-28 08:29:15
I have a controller within MVC3 which needs to return a response code 500 if something goes wrong. I am doing this by returning a view object and setting http response code to equal 500 (I have checked this in firebug and all is working great). public ActionResult http500() { ControllerContext.HttpContext.Response.StatusCode = 500; ControllerContext.HttpContext.Response.StatusDescription = "An error occurred whilst processing your request."; return View(); } The problem I have now is I need to be able to write a unit test which checks the response code. I have tried accessing the response code

Moq C# built in class

早过忘川 提交于 2019-11-28 07:56:12
问题 I'm trying to mock out the SearchResultCollection class. However, when I try to intercept a call to the PropertiesLoaded getter, my test throws an Exception: System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: x => x.PropertiesLoaded My Code: Mock<SearchResultCollection> searchResultMock = new Mock<SearchResultCollection>(); // Set up collection that will be returned string[] tempData = { "one", "two" }; searchResultMock.SetupGet(x => x.PropertiesLoaded)