moq

Moq: Unable to cast to interface

雨燕双飞 提交于 2019-12-05 09:50:58
earlier today I asked this question . So since moq creates it's own class from an interface I wasn't able to cast it to a different class. So it got me wondering what if I created a ICustomPrincipal and tried to cast to that. This is how my mocks look: var MockHttpContext = new Mock<HttpContextBase>(); var MockPrincipal = new Mock<ICustomPrincipal>(); MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object); In the method I am trying to test the follow code gives the error(again): var user = (ICustomPrincipal)httpContext.User; The error is the following: Unable to cast object of

Mocking a method to throw an exception (moq), but otherwise act like the mocked object?

那年仲夏 提交于 2019-12-05 08:20:44
问题 I have a Transfer class, simplified it looks like this: public class Transfer { public virtual IFileConnection source { get; set; } public virtual IFileConnection destination { get; set; } public virtual void GetFile(IFileConnection connection, string remoteFilename, string localFilename) { connection.Get(remoteFilename, localFilename); } public virtual void PutFile(IFileConnection connection, string localFilename, string remoteFilename) { connection.Get(remoteFilename, localFilename); }

What steps to get rid of Collection was modified; enumeration operation may not execute. Error?

最后都变了- 提交于 2019-12-05 08:16:33
Our programming involves some Mock testing using In-Memory Data. Therefore, we implemented the following code that would first create In-Memory Data of Customer objects // Let us create some in-memory data // Create a list of Customer List<Customer> listOfCustomers = new List<BlahBlahExample.Domain.Objects.Customer>() { new Customer { CustomerID = "1 ",Orders = new HashSet<Order>(), CustomerDemographics = new HashSet<CustomerDemographic>(), CompanyName = "Chicago Bulls", ContactName = "Michael Jordan", ContactTitle = "top basket ball player", Address = "332 testing lane", City = "Chicago",

Dynamically calling Moq Setup() at runtime

泄露秘密 提交于 2019-12-05 08:14:58
I want to create a factory that will create commonly mocked objects for my unit tests. I've already managed to set up my tests so I can mock up a Linq2Sql DataContext and return an in memory table instead of hitting the database. I set it up like this: _contactsTable = new InMemoryTable<Contact>(new List<Contact>()); _contactEmailsTable = new InMemoryTable<ContactEmail>(new List<ContactEmail>()); // repeat this for each table in the ContactsDataContext var mockContext = new Mock<ContactsDataContext>(); mockContext.Setup(c => c.Contacts).Returns(_contactsTable); mockContext.Setup(c => c

Can I access the full power of Autofac in UnitTests, using the Moq integration

為{幸葍}努か 提交于 2019-12-05 08:09:52
My project (which happens built on top of Orchard , though I don't think that's relevant) uses Autofac . I am writing unit tests in which I want to stub out any dependencies using Moq , and I'm using the Autofac/Moq integration to achieve this. This is fine for any simple dependencies that are being passed in as constructor arguments. (Autofac documentation provides details of how to achieve this here ). But because I don't ever have a containerBuilder, I don't see how to use a lot of Autofac's power - lamda registration , registering generics , marking properties to be auto-wired . etc. Am I

Moq - How to mock web service call?

…衆ロ難τιáo~ 提交于 2019-12-05 06:52:58
The using below hits an external resource that I do not want to actually hit. I want to test someResult and the code that uses it, but every time I run my unit test, this code still tries to hit the real web service. How do I use moq to fake the real call to the web service, but not mock the rest of the code within the using? public IMyInterface.SomeMethod() { // hits a web service using ( mySoapClient client = new mySoapClient() ) { var someResult = client.DoSomething(); ... ... } } [TestMethod()] public void SomeMethodTest() { IMyInterface target = new MyInterface(); target.SomeMethod(); //

Proper way to verify parameters being passed to a Mock are set as expected

社会主义新天地 提交于 2019-12-05 06:32:39
Is it acceptable to do asserts in your callbacks if you later verify that the methods were called? Is this the preferred way of making sure my mock is getting the expected parameters passed to it, or should I set a local variable in my callback and do the asserts on that instance? I have a situation where I have some logic in a Presenter class that derives values based on inputs and passes them to a Creator class. To test the logic in the Presenter class I want to verify that the proper derived values are observed when the Creator is called. I came up with the example below that works, but I'm

How to use Moq to return a List of data or values?

喜你入骨 提交于 2019-12-05 04:18:17
Can anyone tell me how to return List of data using mock object using Moq framework and assigning the so returned list of data to another List<> variable.?? public class SomeClass { public virtual List<int> GimmeSomeData() { throw new NotImplementedException(); } } [TestClass] public class TestSomeClass { [TestMethod] public void HowToMockAList() { var mock = new Mock<SomeClass>(); mock.Setup(m => m.GimmeSomeData()).Returns(() => new List<int> {1, 2, 3}); var resultList = mock.Object.GimmeSomeData(); CollectionAssert.AreEquivalent(new List<int>{1,2,3},resultList); } } @Richard Banks gave the

EF6 DbSet<T> returns null in Moq

最后都变了- 提交于 2019-12-05 04:09:17
I have a typical Repository Pattern setup in my application with a DbContext (EF6): public class MyDbContext : EFContext<MyDbContext> { public MyDbContext () { } public virtual DbSet<CartItem> Cart { get; set; } and a repository: public class GenericEFRepository<TEntity, TContext> where TEntity : class, new() where TContext : EFContext<TContext> { private readonly TContext _context; public GenericEFRepository(TContext context) { _context = context; } //... public virtual TEntity Insert(TEntity item) { return _context.Set<TEntity>().Add(item); } I'm testing this with Moq 4.2 (following this

Moq - Non-overridable members may not be used in setup / verification expressions

拥有回忆 提交于 2019-12-05 03:35:18
I'm new to Moq. I'm mocking a PagingOptions class. Here is how the class looks like: public class PagingOptions { [Range(1, 99999, ErrorMessage = "Offset must be greater than 0.")] public int? Offset { get; set; } [Range(1, 100, ErrorMessage = "Limit must be greater than 0 and less than 100.")] public int? Limit { get; set; } public PagingOptions Replace(PagingOptions newer) { return new PagingOptions { Offset = newer.Offset ?? Offset, Limit = newer.Limit ?? Limit }; } } Here is my mock version of the class, var mockPagingOptions = new Mock<PagingOptions>(); mockPagingOptions.Setup(po => po