moq

Mocking classes that implement IQueryable with Moq

让人想犯罪 __ 提交于 2019-12-08 22:43:42
问题 I spent an evening trying to mock an object that implements IQueryable: public interface IRepo<T> : IQueryable<T> { } The best I could come up with is something like this: var items = new Item[] {}.AsQueryable(); var repo = new Mock<IRepo>(); repo.Setup(r => r.GetEnumerator()).Returns(items.GetEnumerator()); repo.Setup(r => r.Provider).Returns(items.Provider); repo.Setup(r => r.ElementType).Returns(items.ElementType); repo.Setup(r => r.Expression).Returns(items.Expression); Is there a more

Why doesn't Moq run the overridden ToString method?

╄→尐↘猪︶ㄣ 提交于 2019-12-08 21:21:09
问题 In the following code why does mockTest.ToString() return Null? EDIT: Added comment into example code to show how to fix the problem. Public Sub Main() Try Dim test = New TestClass If test.ToString <> "stackoverflow rules" Then Throw New Exception("Real Failed: Actual value: <" + test.ToString + ">") End If Dim mock = New Moq.Mock(Of TestClass)() mock.SetupGet(Function(m As TestClass) m.Name).Returns("mock value") ' As per Mark's accepted answer this is the missing line of ' of code to make

Moq with Autofac Func<Owned<IClass>>

◇◆丶佛笑我妖孽 提交于 2019-12-08 18:50:00
问题 I'm trying to swtich some code I have to use Owned Instances instead of passing around the container. public class SyncManager { private Func<Owned<ISynchProcessor>> _syncProcessor = null; public SyncManager(Func<Owned<ISynchProcessor>> syncProcessor) { _syncProcessor = syncProcessor; } private void Handle() { using (var service = _syncProcessor()) { service.Value.Process(); } } } Now I want to Moq the Func< Owned> and inject it but the calls to _syncProcessor() don't resolve anything So I

AutoConfiguredMoqCustomization and unsettable properties

笑着哭i 提交于 2019-12-08 17:38:24
问题 How do I force AutoFixture, that has been configured with AutoConfiguredMoqCustomization, to automatically mock interfaces and its read-only properties? To make things clear, let's assume I have such an interface: public interface A { int Property {get;} } and such class: public class SomeClass { public SomeClass(A dependency) {} } What I want is to have dependency resolved to a mock that will return something in dependency.Property : var fixture = new Fixture().Customize(new

Moq how determine a method was called with a list containing certain values [duplicate]

大城市里の小女人 提交于 2019-12-08 15:50:07
问题 This question already has answers here : MOQ - check a method is called with only specific parameters (3 answers) Closed 6 years ago . Hi say I have a method with the following signature: public void GeneratePaymentAdvise(IList<int> paymentIds) and this is called by another method: public void UpdatePaymentStatus(IList<int> paymentIds, IPaymentLogic paymentLogic) { ... paymentLogic.GeneratePaymentStatus(paymentIds); ... } So in a unit test I want to make sure this was called. Using moq: var

Mocking a type with an internal constructor using Moq

僤鯓⒐⒋嵵緔 提交于 2019-12-08 15:40:54
问题 I'm trying to mock a class from the Microsoft Sync Framework. It only has an internal constructor. When I try the following: var fullEnumerationContextMock = new Mock<FullEnumerationContext>(); I get this error: System.NotSupportedException: Parent does not have a default constructor. The default constructor must be explicitly defined. This is the stack trace: System.Reflection.Emit.TypeBuilder.DefineDefaultConstructorNoLock(MethodAttributes attributes) System.Reflection.Emit.TypeBuilder

Why is my mock set empty?

戏子无情 提交于 2019-12-08 15:24:28
I am just starting to learn about unit testing and mocking. I've spent all day reading different tutorials, trying to find the best one to practice with. I've settled on Testing with a mocking framework (EF6 onwards) as it is using EF6 (modern), as well as what seems to be a very popular mocking framework (Moq). Also, it is quite vanilla and hosted on the MSDN webset. It has to be decent, right? I've set up a project exactly as specified in the example and am running the debugger through the test examples to make sure I understand what is going on. The test I'm working through is as follows:

Autofixture and Moq v4

删除回忆录丶 提交于 2019-12-08 15:15:29
问题 I installed Autofixture and Moq using Nuget.So I have moq version 4. When running the following code var fixture = new Fixture().Customize(new AutoMoqCustomization()); fixture.CreateAnonymous<ISomething>(); the following error shows up System.IO.FileLoadException : Could not load file or assembly 'Moq, Version=3.1.416.3, Culture=neutral, PublicKeyToken=69f491c39445e920' I've also tried redirected it to the v4,but with no luck. <configuration> <runtime> <assemblyBinding> <dependentAssembly>

Moq how to replace obsolete expression

爱⌒轻易说出口 提交于 2019-12-08 14:28:07
问题 I'm using Moq in my code. I wrote an expression like: mockInvoice.VerifySet(x => x.InvoiceAttachmentId, Times.Once()); Where InvoiceAttachmentId is a property on my Invoice. It works fine but I get the warning: Moq.MockExtensions.VerifySet(Moq.Mock, System.Linq.Expressions.Expression>, Moq.Times)' is obsolete: 'Replaced by VerifySet(Action, Times)' Can anyone tell me how to rewrite it to satisfy the compiler and get rid of the warning? I'm unsure how to make the replacement to Action. 回答1:

Why is my mock set empty?

耗尽温柔 提交于 2019-12-08 07:46:42
问题 I am just starting to learn about unit testing and mocking. I've spent all day reading different tutorials, trying to find the best one to practice with. I've settled on Testing with a mocking framework (EF6 onwards) as it is using EF6 (modern), as well as what seems to be a very popular mocking framework (Moq). Also, it is quite vanilla and hosted on the MSDN webset. It has to be decent, right? I've set up a project exactly as specified in the example and am running the debugger through the