moq

Moq with lambda expressions?

穿精又带淫゛_ 提交于 2019-12-23 20:14:35
问题 I am trying to test an application service using Moq 4.0.10827 (on NuGet) and need to query a repository: public class MyService { Repository<MyObject> _Repo; public MyObject Get (string SomeConstraint) { return _Repo .GetTheFirstOneOrReturnNull (M => M.Constraint.Equals ( SomeContraint, StringComparison.InvariantCultureIgnoreCase )); // GetTheFirstOneOrReturnNull takes a Func<MyObject, bool> } } How do I replicate the lambda expression with Moq? I keep getting the "Unsupported expression"

Using Test Doubles with DbEntityEntry and DbPropertyEntry

落花浮王杯 提交于 2019-12-23 19:23:16
问题 I am using the new Test Doubles in EF6 as outlined here from MSDN . VS2013 with Moq & nUnit. All was good until I had to do something like this: var myFoo = context.Foos.Find(id); and then: myFoo.Name = "Bar"; and then : context.Entry(myFoo).Property("Name").IsModified = true; At this point is where I get an error: Additional information: Member 'IsModified' cannot be called for property 'Name' because the entity of type 'Foo' does not exist in the context. To add an entity to the context

How to mock LINQ to Entities helpers such as 'SqlFunctions.StringConvert()'

烂漫一生 提交于 2019-12-23 15:08:42
问题 I am using EF 4 and is trying to unit test the follow line using Moq: var convertError = models .Where(x => SqlFunctions.StringConvert((decimal?) (x.convert ?? 0)) == "0") .Any(); and it seems like SqlFunctions.StringConvert() will throw if it detects the context is mocked. It gives an error saying: This function can only be invoked from LINQ to Entities Is it possible to tell SqlFunctions.StringConvert to return a mock object so I can get rid of this error? 回答1: No it is not possible because

Unit testing LINQ to SQL CRUD operations using Moq

有些话、适合烂在心里 提交于 2019-12-23 13:04:52
问题 I've had a look around at other questions, but nothing really matches what I'm looking for... mainly because I'm not 100% certain on what it is I'm looking for! Basically I'm working on a new project at the moment, I've created my abstraction layer for DB entities and set the DAC to be repositories. I'm wanting to use unit testing for this with Mock objects, however I've hit a intellectual wall with CRUD (specifically C) operations, my unit test class: [TestClass] public class RepositoryTest

Mocking IDocumentQuery in Unit Test that uses SQL Queries

强颜欢笑 提交于 2019-12-23 13:01:41
问题 I am using unit tests to test DocumentDBRepository class. I followed this post as an example for the SQL queries use case. But it shows error of Message: System.InvalidCastException : Unable to cast object of type 'System.Linq.EnumerableQuery to type 'Microsoft.Azure.Documents.Linq.IDocumentQuery Here's my code for DocumentDBRepository class private IDocumentQuery<T> GetQueryBySQL(string queryStr) { var uri = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); var feedOptions

RestSharp Unit Test NUnit Moq RestResponse null reference exception

余生颓废 提交于 2019-12-23 12:24:28
问题 I'm having some challenges trying to use Moq with RestSharp. Maybe it's it my misunderstanding of Moq but for some reason I keep on getting a null reference exception when trying to Mock a RestResponse. Here is my unit test. [Test] public void GetAll_Method_Throws_exception_if_response_Data_is_Null() { var restClient = new Mock<IRestClient>(); restClient.Setup(x => x.Execute(It.IsAny<IRestRequest>())) .Returns(new RestResponse<RootObjectList> { StatusCode = HttpStatusCode.OK, Content = null }

EF6 - Cannot Mock Return Value for ObjectResult<T> for Unit Test

十年热恋 提交于 2019-12-23 12:18:20
问题 I have code similar to this in a method that I'm trying to unit test: return _context.usp_get_Some_Data(someStringParam).FirstOrDefault(); The stored proc call return type: ObjectResult<usp_get_Some_Data_Result>. In my unit test, I'm trying to do something like this (using NUnit and Moq): var procResult = new ObjectResult<usp_get_Some_Data_Result>(); mockContext.Setup(m => m.usp_get_Some_Data(It.IsAny<string>())) .Returns(procResult); However, I'm not able to create an instance of

Autofixture and WebApi Controller

一曲冷凌霜 提交于 2019-12-23 12:04:12
问题 I am using AutoFixture to try to test my controllers for a WebApi site. I am using the AutoData feature with Moq as noted on Ploeh's blog. My controller takes an IDepartmentManager in the constructor. Here is my test: [Theory, AutoMoqData] public void GetCallsManagerCorrectly( [Frozen]Mock<IDepartmentManager> departmentManagerMock, DepartmentsController sut) { // Fixture setup // Exercise system sut.Get(); // Verify outcome departmentManagerMock.Verify(d => d.GetAllDepartments(), Times

Moq framework Func<T,T>

人盡茶涼 提交于 2019-12-23 11:57:05
问题 I am new with Moq and TDD and what I am trying to do is to set up method on repository interface. Here is full story. I have a domain entity class called Tenant with property BusinessIdentificationNumber public class Tenant:EntityBase<Tenant>,IAggregateRoot { ... public string BusinessIdentificationNumber {get;set;} ... } Next I have repository for this entity which interface is like public interface IRepository<T> { ... T FindBy(Func<T,bool> func); ... } where the problem is, I use a domain

Moq Invocation was not performed on the mock

删除回忆录丶 提交于 2019-12-23 11:53:25
问题 Trying to understand the use of verifySet etc... but unless I do a workaround I cannot get it to work. public interface IProduct { int Id { get; set; } string Name { get; set; } } public void Can_do_something() { var newProduct = new Mock<IProduct>(); newProduct.SetupGet(p => p.Id).Returns(1); newProduct.SetupGet(p => p.Name).Returns("Jo"); //This fails!! why is it because I have not invoked it newProduct.VerifySet(p => p.Name, Times.AtLeastOnce()); //if I do this it works newProduct.Object