moq

Moq is throwing Invalid setup on a non-overridable member, when class is in Service Project

空扰寡人 提交于 2019-12-12 14:45:08
问题 I am trying to unit test this class ServizioController : public class ServizioController : IServizioController { public virtual void PerformAction(Super.Core.Servizio servizio) { } public virtual bool Start() { throw new NotImplementedException(); } public virtual bool Stop() { throw new NotImplementedException(); } public virtual bool Continue() { throw new NotImplementedException(); } } It goes fine if this class is part of a test or a library project. But When it is in the Service Project,

Moq chaining expressions results in parameter count mismatch

倾然丶 夕夏残阳落幕 提交于 2019-12-12 14:41:16
问题 I'm attempting to mock a repository using Moq. I have found multiple questions with similar issues but none that I found were able to solve the issue I'm having. So I'm using a repository that can be downloaded here. More specifically, the repository itself can be viewed here and the query extensions I'm using can be seen here. This is what my tests setup looks like: // A List<> of fakes. this.containers = Builder<Container>.CreateListOfSize(10).Build(); // Here I'm trying to set up the mock

Should this case of Assert.AreSame return true?

一世执手 提交于 2019-12-12 14:36:31
问题 I am testing a repository pattern I have created and I use Moq package to mock my objects. I wanted to test references from 2 objects, but the result kind of surprises me. Here is the test: Mock<Repository<Web_Documents>> moqRepo; Mock<Repository<Web_Documents>> moqRepo2; public void ObjEqGetTest() { //context is DBContext and has been initialized using [TestInitialize] annotation moqRepo = new Mock<Repository<Web_Documents>>(context); moqRepo2 = new Mock<Repository<Web_Documents>>(context);

How to setup a simple Unit Test with Moq?

两盒软妹~` 提交于 2019-12-12 14:30:35
问题 I'm new to Moq and not quite sure why this won't run. Repository Interface using System.Collections.Generic; public interface IRepository { IEnumerable<string> list(); } Service Interface using System.Collections.Generic; public interface IService { IEnumerable<string> AllItems(); } Service Class using System.Collections.Generic; public class Service : IService { private IRepository _repository; public Service(IRepository repository) { this._repository = repository; } public IEnumerable

Verify method was called with certain linq expression (moq)

为君一笑 提交于 2019-12-12 12:31:18
问题 Can't figure out the syntax. //class under test public class CustomerRepository : ICustomerRepository{ public Customer Single(Expression<Func<Customer, bool>> query){ //call underlying repository } } //test var mock = new Mock<ICustomerRepository>(); mock.Object.Single(x=>x.Id == 1); //now need to verify that it was called with certain expression, how? mock.Verify(x=>x.Single(It.Is<Expression<Func<Customer, bool>>>(????)), Times.Once()); Please help. 回答1: Hmmm, you can verify that the lambda

Using Autofac and Moqs with Delegate Factories

一笑奈何 提交于 2019-12-12 12:28:38
问题 I am trying to unit test a class that uses factory injection. I have a class that instantiates two copies of the same object (with different config) to control hardware. I'm trying to test the behaviour of the classes while simulating the hardware calls. I've injected a set of factory delegates into the constructors so that the class can instantiate the hardware classes as required. However I just can't work out how to control or create factory methods within the Autofac.Extras.Moq package.

How to mock HttpClientCertificate?

送分小仙女□ 提交于 2019-12-12 12:09:17
问题 I am trying to unit test an action filter I wrote. I want to mock the HttpClientCertificate but when I use MOQ I get exception. HttpClientCertificate doesnt have a public default constructor. code: //Stub HttpClientCertificate </br> var certMock = new Mock<HttpClientCertificate>(); HttpClientCertificate clientCertificate = certMock.Object; requestMock.Setup(b => b.ClientCertificate).Returns(clientCertificate); certMock.Setup(b => b.Certificate).Returns(new Byte[] { }); 回答1: This is the most

Moq confusion - Setup() v Setup<>()

丶灬走出姿态 提交于 2019-12-12 10:46:49
问题 I have a mock being created like this: var mock = new Mock<IPacket>(MockBehavior.Strict); mock.Setup(p => p.GetBytes()).Returns(new byte[] { }).Verifiable(); The intellisense for the Setup method says this: "Specifies a setup on the mocked type for a call to a void returning method." But the mocked method p.GetBytes() does not return void, it returns a byte array. Alternatively another Setup method is defined as Setup<>, and I can create my mock like this: var mock = new Mock<IPacket>

Assigning out parameters in Moq for methods that return void

戏子无情 提交于 2019-12-12 10:42:42
问题 In this question, I found an this answer that seems to be the best way to solve the problem to me. The provided code assumes that the function that is mocked returns a value: bool SomeFunc(out ISomeObject o); However, the object I want to mock has an out function as follows: void SomeFunc(out ISomeObject o); The relevant code fragment from the mentioned answer: public delegate void OutAction<TOut>(out TOut outVal); public static IReturnsThrows<TMock, TReturn> OutCallback<TMock, TReturn, TOut>

System.NotSupportedException: Unsupported expression: p => (p.UserProfileId == 1)

╄→尐↘猪︶ㄣ 提交于 2019-12-12 10:03:15
问题 I have a test that uses System.Func expressions. It should be pretty straight forward, but the test keeps on failing. Test: [TestMethod] public void GetUser() { var username = "john@resilientplc.com"; var user = new User() { UserId = 1, Username = username, Password = "123456789" }; someDataMock.Setup(s => s.GetUser(p => p.UserId == 1)).Returns(user); var result = userProfileModel.GetUser(user.UserId); Assert.AreEqual(user, result); } Implementation UserProfileModel : public User GetUser(long