moq

Moq: Invalid setup on a non-overridable member: x => x.GetByTitle(“asdf”)

喜夏-厌秋 提交于 2019-11-28 07:54:04
Not sure how I can fix this, trying to do a unit test on the method "GetByTitle" Here are my definitions: public class ArticleDAO : GenericNHibernateDAO(IArticle, int>, IArticleDAO { public IArticle GetByTitle(string title) { IQuery query = Session.CreateQuery("...") return query.UniqueResult<IArticle>(); } } public interface IArticleDAO { IArticle GetByTitle(string title); } unit test: [Test] public void can_load_by_title() { _mockDaoFactory.Setup(x => x.GetArticleDao()) .Returns(_mockArticleDao.Object); _mockArticleDao.Setup(x => x.GetByTitle("some title")) .Returns(article1.Object);

Mocking new Microsoft Entity Framework Identity UserManager and RoleManager

最后都变了- 提交于 2019-11-28 07:09:46
Has anyone come up with a successful mocking solution for UserManager and RoleManager ? I have been beating my head against a wall all day. All I want to do is mock the objects to use an in memory collection rather than hitting the Entity Framework data store. I've scoured the internet and tried several different approaches using MOQ. I was under the impression that the new stuff was much easier to test. Am I missing something? danludwig Alternatively, you can mock the IUserStore<TUser> interface that UserManager accepts as an argument. var userStore = new Mock<IUserStore<ApplicationUser>>();

Mocking generic method call for any given type parameter

非 Y 不嫁゛ 提交于 2019-11-28 07:08:58
I have an interface public interface IDataProvider { T GetDataDocument<T>(Guid document) where T:class, new() } I'd like to mock it in a way, that it would just return a new instance of a given type, regardless of the exact type, something like: myMock.Setup(m => m.GetDataDocument<It.IsAny<Type>()>(It.IsAny<Guid>())) .Returns(() => new T()); (which doesn't work of course, because I cannot just give any type parameter to moq, and I can't know which type must be returned. Any ideas on this one? Mark Coleman Instead of using a mock, maybe your case would be better to use a Stub . public class

Using moq to mock only some methods

ⅰ亾dé卋堺 提交于 2019-11-28 07:07:38
I have the following method: public CustomObect MyMethod() { var lUser = GetCurrentUser(); if (lUser.HaveAccess) { //One behavior } else { //Other behavior } //return CustomObject } I want to mock IMyInterface.GetCurrentUser , so that while calling MyMethod I could get to one of the code paths to check it. How to do that with Moq? I'm doing the following thing: var moq = new Mock<IMyInterface>(); moq.Setup(x => x.GetCurrentUser()).Returns(lUnauthorizedUser); //act var lResult = moq.Object.MyMethod(); But for some reason lResult is always null and when I'm trying to get into MyMethod in debug I

Mocking objects with Moq when constructor has parameters

て烟熏妆下的殇ゞ 提交于 2019-11-28 07:06:57
I have an object I'm trying to mock using moq. The object's constructor has required parameters: public class CustomerSyncEngine { public CustomerSyncEngine(ILoggingProvider loggingProvider, ICrmProvider crmProvider, ICacheProvider cacheProvider) { ... } } Now I'm trying to create the mock for this object using either moq's v3 "setup" or v4 "Mock.Of" syntax but can't figure this out... everything I'm trying isn't validating. Here's what I have so far, but the last line is giving me a real object, not the mock. The reason I'm doing this is because I have methods on the CustomerSyncEngine I want

Mocking generic methods in Moq without specifying T

≯℡__Kan透↙ 提交于 2019-11-28 06:43:35
I have an interface with a method as follows: public interface IRepo { IA<T> Reserve<T>(); } I would like to mock the class that contains this method without having to specify Setup methods for every type it could be used for. Ideally, I'd just like it to return a new mock<T>.Object . How do I achieve this? It seems my explanation was unclear. Here's an example - this is possible right now, when I specify the T (here, string): [TestMethod] public void ExampleTest() { var mock = new Mock<IRepo>(); mock.Setup(pa => pa.Reserve<string>()).Returns(new Mock<IA<string>>().Object); } What I would like

Verifying a method was called

杀马特。学长 韩版系。学妹 提交于 2019-11-28 06:22:54
Using Moq, I have a very odd issue where the setup on a mock only seems to work if the method I am setting up is public. I don't know if this is a Moq bug or if I just have this wrong (newbie to Moq). Here is the test case: public class TestClass { public string Say() { return Hello(); } internal virtual string Hello() { return ""; } } [TestMethod] public void Say_WhenPublic_CallsHello() { Mock<TestClass> mock = new Mock<TestClass>(); mock.Setup(x => x.Hello()).Returns("Hello World"); string result = mock.Object.Say(); mock.Verify(x => x.Hello(), Times.Exactly(1)); Assert.AreEqual("Hello World

Mocking The RouteData Class in System.Web.Routing for MVC applications

淺唱寂寞╮ 提交于 2019-11-28 06:16:52
I'm trying to test some application logic that is dependent on the Values property in ControllerContext.RouteData. So far I have // Arrange var httpContextMock = new Mock<HttpContextBase>(MockBehavior.Loose); var controllerMock = new Mock<ControllerBase>(MockBehavior.Loose); var routeDataMock = new Mock<RouteData>(); var wantedRouteValues = new Dictionary<string, string>(); wantedRouteValues.Add("key1", "value1"); var routeValues = new RouteValueDictionary(wantedRouteValues); routeDataMock.SetupGet(r => r.Values).Returns(routeValues); <=== Fails here var controllerContext = new

How to throw a SqlException when needed for mocking and unit testing?

半世苍凉 提交于 2019-11-28 06:07:50
I am trying to test some exceptions in my project and one of the Exceptions I catch is SQlException . It seems that you can't go new SqlException() so I am not sure how I can throw an exception especially without somehow calling the database (and since these are unit tests it is usually advised not to call the database since it is slow). I am using NUnit and Moq, but I am not sure how to fake this. Responding to some of the answers that seem to all be based on ADO.NET, note that I am using Linq to Sql. So that stuff is like behind the scenes. More info as requested by @MattHamilton: System

Settings variable values in a Moq Callback() call

瘦欲@ 提交于 2019-11-28 05:37:47
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<IFooBar>() call. Is this not working as I expect? I would think FilterCollection would be called and then