moq

Mocking Guid.NewGuid()

筅森魡賤 提交于 2019-12-01 16:52:40
问题 Suppose I have the following entity: public class User { public int Id { get; set; } public string Username { get; set; } public Guid UserGuid { get; set; } public Guid ConfirmationGuid { get; set; } } And the following interface method: void CreateUser(string username); Part of the implementation should create two new GUIDs: one for UserGuid , and another for ConfirmationGuid . They should do this by setting the values to Guid.NewGuid() . I already have abstracted Guid.NewGuid() using an

Mocking Guid.NewGuid()

旧城冷巷雨未停 提交于 2019-12-01 16:46:29
Suppose I have the following entity: public class User { public int Id { get; set; } public string Username { get; set; } public Guid UserGuid { get; set; } public Guid ConfirmationGuid { get; set; } } And the following interface method: void CreateUser(string username); Part of the implementation should create two new GUIDs: one for UserGuid , and another for ConfirmationGuid . They should do this by setting the values to Guid.NewGuid() . I already have abstracted Guid.NewGuid() using an interface: public interface IGuidService { Guid NewGuid(); } So I can easily mock this when only one new

Nuget cannot find newer dependency

别说谁变了你拦得住时间么 提交于 2019-12-01 16:34:20
问题 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

When using Moq Verify() method invocation count, have failing test's error message contain actual method invocation count using Moq

本小妞迷上赌 提交于 2019-12-01 15:44:10
Consider the following, where I am testing that an injected dependency's method is called a specific number of times: [Fact] public void WhenBossTalksEmployeeBlinksTwice() { // arrange var employee = new Mock<IEmployee>(); employee.Setup(e => e.Blink()); var boss = new Boss(employee.Object); // act boss.Talk(); // assert employee.Verify(e => e.Blink(), Times.Exactly(2)); // Passes as expected employee.Verify(e => e.Blink(), Times.Exactly(1)); // Fails as expected } When I force the failing test, the output is: Moq.MockException: Invocation was not performed on the mock 1 times: e => e.Blink()

How to moq a Func

跟風遠走 提交于 2019-12-01 15:15:29
问题 Trying to unit test a class whose constructor takes in a Func. Not sure how to mock it using Moq. public class FooBar { public FooBar(Func<IFooBarProxy> fooBarProxyFactory) { _fooBarProxyFactory = fooBarProxyFactory; } } [Test] public void A_Unit_Test() { var nope = new Mock<Func<IFooBarProxy>>(); var nope2 = new Func<Mock<IFooBarProxy>>(); var fooBar = new FooBar(nope.Object); var fooBar2 = new FooBar(nope2.Object); // what's the syntax??? } 回答1: figured it out public interface IFooBarProxy

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

五迷三道 提交于 2019-12-01 15:04:50
问题 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

Is the moq project dead? Is it wise for me to invest in learning it? [closed]

落花浮王杯 提交于 2019-12-01 15:03:25
I am fairly new to mocking frameworks and was trying to decide which one will be a good bet to start working on. I have been looking at this question about the best mocking framework, and I can see a lot of people preferring moq, but when i saw the moq project's change list , i can see that it has not been updated for almost an year now. Is moq project dead? if yes, which will be a good mocking framework to start with today? Moq is not dead, it's heavily used and it has a steady download count both via nuget and google code. It's just that it works so great that there hasn't been any show

Verifying generic method called using Moq

梦想与她 提交于 2019-12-01 14:54:00
问题 I'm having trouble verifying that mock of IInterface.SomeMethod<T>(T arg) was called using Moq.Mock.Verify . I'm can verify that method was called on a "Standard" interface either using It.IsAny<IGenericInterface>() or It.IsAny<ConcreteImplementationOfIGenericInterface>() , and I have no troubles verifying a generic method call using It.IsAny<ConcreteImplementationOfIGenericInterface>() , but I can't verify a generic method was called using It.IsAny<IGenericInterface>() - it always says that

Getting past entity framework BeginTransaction

假装没事ソ 提交于 2019-12-01 14:43:44
问题 I am trying to make sense of mocking in unit testing and to integrate the unit testing process to my project. So I have been walking thru several tutorials and refactoring my code to support mocking, anyway, I am unable to pass the tests, because the DB method I am trying to test is using a transaction, but when creating a transaction, I get The underlying provider failed on Open. Without transaction everything works just fine. The code I currently have is: [TestMethod] public void Test1() {

How to unit test methods that use System.Web.Security.Membership inside?

流过昼夜 提交于 2019-12-01 14:39:58
问题 I want to test a method to check that it saves a transaction correctly. Inside it calls Membership.GetUser() to verify the user which causes the test to fail each time. Is there any way to mock this so that Membership.GetUser() always returns a valid name? I'm using Moq, C# and ASP.Net 4.5 MVC 回答1: In short, you can't. That's why every call to such a "service" should be hidden behind an abstraction. You can see a sample of that in default MVC template. 回答2: Yes, like Serg said, you can mock