moq

How to make Moq ignore arguments that are ref or out

时光毁灭记忆、已成空白 提交于 2019-12-18 18:38:19
问题 In RhinoMocks, you can just tell your mocks to IgnoreArguments as a blanket statement. In Moq, it seems, you have to specify It.IsAny() for each argument. However, this doesn't work for ref and out arguments. How can I test the following method where I need to Moq the internal service call to return a specific result: public void MyMethod() { // DoStuff IList<SomeObject> errors = new List<SomeObject>(); var result = _service.DoSomething(ref errors, ref param1, param2); // Do more stuff } Test

ASP.NET MVC - Unit testing RenderPartialViewToString() with Moq framework?

不羁的心 提交于 2019-12-18 16:49:36
问题 I'm using this helper method to turn my PartialViewResult into string and returning it as Json - http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/ My problem is that I'm using Moq to mock the controller, and whenever I run unit test that uses this RenderPartialViewToString() method, I got the "Object reference not set to an instance of an object." error on ControllerContext. private ProgramsController GetController() { var mockHttpContext = new Mock

Verify the number of times a protected method is called using Moq

断了今生、忘了曾经 提交于 2019-12-18 14:45:12
问题 In my unit-tests I'm mocking a protected method using Moq, and would like to assert that it is called a certain number of times. This question describes something similar for an earlier version of Moq: //expect that ChildMethod1() will be called once. (it's protected) testBaseMock.Protected().Expect("ChildMethod1") .AtMostOnce() .Verifiable(); ... testBase.Verify(); but this no longer works; the syntax has changed since then and I cannot find the new equivalent using Moq 4.x: testBaseMock

Moq ReturnsAsync() with parameters

百般思念 提交于 2019-12-18 13:52:57
问题 I'm trying to mock a repository's method like that public async Task<WhitelistItem> GetByTypeValue(WhitelistType type, string value) using Moq ReturnsAsync, like this: static List<WhitelistItem> whitelist = new List<WhitelistItem>(); var whitelistRepositoryMock = new Mock<IWhitelistRepository>(); whitelistRepositoryMock.Setup(w => w.GetByTypeValue(It.IsAny<WhitelistType>(), It.IsAny<string>())) .ReturnsAsync((WhitelistType type, string value) => { return (from item in whitelist where item

Mocking virtual readonly properties with moq

一笑奈何 提交于 2019-12-18 13:52:37
问题 I couldn't find a way to do this, though this can be done by hand so why not with moq? 回答1: Given this class public abstract class MyAbstraction { public virtual string Foo { get { return "foo"; } } } you can set up Foo (a read-only property) like this: var stub = new Mock<MyAbstraction>(); stub.SetupGet(x => x.Foo).Returns("bar"); stub.Object.Foo will now return "bar" instead of "foo". 来源: https://stackoverflow.com/questions/1454186/mocking-virtual-readonly-properties-with-moq

What is the purpose of VerifyAll() in Moq?

穿精又带淫゛_ 提交于 2019-12-18 13:52:20
问题 I read the question at What is the purpose of Verifiable() in Moq? and have this question in my mind. Need your help to explain that. 回答1: VerifyAll() is for verifying that all the expectations have been met. Suppose you have: myMock.Setup(m => m.DoSomething()).Returns(1); mySut.Do(); myMock.VerifyAll(); // Fail if DoSomething was not called HTH 回答2: I will try to complete @ema's answer, probably it will give more insights to the readers. Imagine you have mocked object, which is a dependency

How can I run the event handler assigned to a mock?

二次信任 提交于 2019-12-18 12:21:04
问题 I am trying to fire the event handler assigned to my timer mock. How can I test this private method here? public interface ITimer { void Start(); double Interval { get; set; } event ElapsedEventHandler Elapsed; } Client class assigns an event handler to this object. I want to test the logic in this class. _timer.Elapsed += ResetExpiredCounters; And the assigned method is private private void ResetExpiredCounters(object sender, ElapsedEventArgs e) { // do something } I want to have this event

How to add an item to a Mock DbSet (using Moq)

二次信任 提交于 2019-12-18 10:12:36
问题 I'm trying to set up a mock DbSet for testing purposes. I used the tutorial here, http://www.loganfranken.com/blog/517/mocking-dbset-queries-in-ef6/ and slightly modified it so calling GetEnumerator returns a new enumerator each time (another problem i was having). However, I am having difficulty adding items to the DbSet. The output is preCount = 3 postCount = 3. However, I expect it to be precount = 3 postCount = 4. Any help is greatly appreciated. static void Main(string[] args) { Debug

How to Moq Setting an Indexed property

拟墨画扇 提交于 2019-12-18 07:42:40
问题 I'm trying to use mock to verify that an index property has been set. Here's a moq-able object with an index: public class Index { IDictionary<object ,object> _backingField = new Dictionary<object, object>(); public virtual object this[object key] { get { return _backingField[key]; } set { _backingField[key] = value; } } } First, tried using Setup() : [Test] public void MoqUsingSetup() { //arrange var index = new Mock<Index>(); index.Setup(o => o["Key"]).Verifiable(); // act index.Object["Key

Using Moq to set indexers in C#

ⅰ亾dé卋堺 提交于 2019-12-18 04:31:45
问题 I'm having trouble figuring out how to set indexers in C# with Moq. The Moq documentation is weak, and I've done a lot of searching... what I'd like to do is similar in the solution to How to Moq Setting an Indexed property: var someClass = new Mock<ISomeClass>(); someClass.SetupSet(o => o.SomeIndexedProperty[3] = 25); I want to modify the above to work for any index and any value so I can just do something like this: someClass.Object.SomeIndexedProperty[1] = 5; Currently I have the following