moq

Mocking generics that implement multiple interfaces

佐手、 提交于 2019-12-01 19:44:11
Here's my class implementation where the generic is implementing two interfaces... public class ClassA<TGeneric> : where TGeneric: IInterfaceA, IInterfaceB I want to Mock ClassA. However, I can't use var mock = new Mock<Class<A<IInterfaceA>>(); or var mock = new Mock<Class<A<IInterfaceB>>(); since the generic requires implementations of both interfaces. I know you can mock objects with multiple interfaces by using the As() method on the moq, but I don't really have an object here but a generic type. Thoughts? Thanks... You could define an interface that includes both interface A and B (in your

Mocking a Method that uses an Optional Parameter using Moq

浪尽此生 提交于 2019-12-01 19:24:25
问题 I have a message box service that hase the following interface public interface IMessageBoxService { DialogResult DisplayMessage(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1); } It essentally wraps the System.Windows.Forms message box and allows me to Mock parts of my code that show a message box. Now I have a search service for text documents that shows a "No more

Moq DbSet NotImplementedException

狂风中的少年 提交于 2019-12-01 18:48:21
I have a Moq DbSet that has been working until recently, however since the last update of dependencies it keeps throwing a NotImplementedException on IQueryable.Provider Code used as follows: var mockSet = new Mock<DbSet<A>>(); var list = new List<A>(); var queryable = list.AsQueryable(); mockSet.As<IQueryable<A>>().Setup(m => m.Provider).Returns(queryable.Provider); mockSet.As<IQueryable<A>>().Setup(m => m.Expression).Returns(queryable.Expression); mockSet.As<IQueryable<A>>().Setup(m => m.ElementType).Returns(queryable.ElementType); mockSet.As<IQueryable<A>>().Setup(m => m.GetEnumerator())

asp.net mvc: how to mock Url.Content(“~”)?

北城以北 提交于 2019-12-01 18:40:16
anybody knows how to mock Url.Content("~") ? (BTW: I'm using Moq) You are referring to the Url property in the controllers, I presume, which is of type UrlHelper . The only way we have been able to mock this is to extract an IUrlHelper interface, and create a UrlHelperWrapper class that both implements it and wraps the native UrlHelper type. We then define a new property on our BaseController like so: public new IUrlHelper Url { get { return _urlHelper; } set { _urlHelper = value; } } This allows us to create mocks of IUrlHelper and inject them, but in the default case to use an instance of

Using Moq to verify execution of private methods

那年仲夏 提交于 2019-12-01 18:02:54
I want to test the following logic (this is obviously a stripped-down version of my method): public void myPublicMethod(params) { if(some_condition) privateMethod1(); else privateMethod2(); } I have all of the other dependencies in the method mocked out, and I've set this up so that I can guarantee that some_condition is true. What I want to do is verify that my privateMethod1() is called exactly once, and privateMethod2() is not called at all. Is this possible to do with Moq? Here are some notes on the issue: privateMethod1() and privateMethod2() are within the same class as myPublicMethod,

How to mock the controller context with moq

淺唱寂寞╮ 提交于 2019-12-01 17:52:10
I am trying out the MOQ framework and up now I have hit a barrier. The following unit test fails because the actual value of the ViewName property is an empty string. Could anyone point me in the right direction please as to why this is not passing the test? [TestMethod] public void Can_Navigate_To_About_Page() { var request = new Mock<HttpRequestBase>(); request.Setup(r => r.HttpMethod).Returns("GET"); var mockHttpContext = new Mock<HttpContextBase>(); mockHttpContext.Setup(c => c.Request).Returns(request.Object); var controllerContext = new ControllerContext(mockHttpContext.Object, new

Using Moq to verify execution of private methods

ぐ巨炮叔叔 提交于 2019-12-01 17:44:27
问题 I want to test the following logic (this is obviously a stripped-down version of my method): public void myPublicMethod(params) { if(some_condition) privateMethod1(); else privateMethod2(); } I have all of the other dependencies in the method mocked out, and I've set this up so that I can guarantee that some_condition is true. What I want to do is verify that my privateMethod1() is called exactly once, and privateMethod2() is not called at all. Is this possible to do with Moq? Here are some

How can I convert a Predicate<T> to an Expression<Predicate<T>> to use with Moq?

本秂侑毒 提交于 2019-12-01 17:09:09
Please help this Linq newbie! I'm creating a list inside my class under test, and I would like to use Moq to check the results. I can easily put together a Predicate which checks the results of the list. How do I then make that Predicate into an Expression? var myList = new List<int> {1, 2, 3}; Predicate<List<int>> myPredicate = (list) => { return list.Count == 3; // amongst other stuff }; // ... do my stuff myMock.Verify(m => m.DidStuffWith(It.Is<List<int>>( ??? ))); ??? needs to be an Expression<Predicate<List<int>>> if you can get your head round that many generics. I've found answers which

Nuget cannot find newer dependency

拜拜、爱过 提交于 2019-12-01 17:06:06
I've just created a new project in ASP 5 MVC 6 beta8 and a compatible class library for tests. The problem occurs in this new "Web Class Library" project that I intended to use for tests. This is what my project.json looks like: { "version": "1.0.0-*", "description": "ClassLibrary1 Class Library", "authors": [ "Me" ], "tags": [ "" ], "projectUrl": "", "licenseUrl": "", "frameworks": { "dnx451": { } }, "dependencies": { "AutoFixture": "3.36.9", "AutoFixture.AutoMoq": "3.36.9", "Moq": "4.2.1510.2205" } } During compilation I get the following error: Severity Code Description Project File Line

moq - how to verify method has not been called if the class swallows exceptions

会有一股神秘感。 提交于 2019-12-01 17:01:32
I am trying to test a fairly complex class using Moq and am running into a problem. I am trying to verify that a method does NOT get called, and usually this is simple to do by setting MockBehavior.Strict, but here however the class has its own error reporting mechanism so it swallows the exception being thrown by Moq. .VerifyAll method at the end of the test also passes fine, which is really weird. Is this a bug in Moq, are there any workarounds? I've also tried setting up a callback on this method and doing Assert.Fail inside of it, but as this gets swallowed as well, the testing framework