moq

What is the DynamicProxyGenAssembly2 assembly?

女生的网名这么多〃 提交于 2019-12-04 23:58:36
I use Moq for my unit tests. To test internal interfaces I have to declare the InternalsVisibleTo attribute to the DynamicProxyGenAssembly2 assembly. We run our tests obfuscated, which is useful because we could found some problems with obfuscation with this approach. Caused by the DynamicProxyGenAssembly2 assembly my component is not obfuscated. Therefore I need to know what is the DynamicProxyGenAssembly2 assembly? I could not found it anywhere on my disk. Is it compiled into the moq.dll or is it generated at runtime? Maybe if I know what this assembly I we coul find a way to workaround my

Can not instantiate proxy…Could not find a parameterless constructor

百般思念 提交于 2019-12-04 23:51:23
I am trying to create a unit test using Moq which tests the MongoDB.AspNet.Identity V2 provider. This line is giving me grief: var appUser = new Mock<PreRegistrationMVC.Models.ApplicationUser>(); var userStore = new Mock<MongoDB.AspNet.Identity.UserStore<PreRegistrationMVC.Models.ApplicationUser>>(); It seems the userStore won't instantiate properly here is the error. Castle.DynamicProxy.InvalidProxyConstructorArgumentsException was unhandled by user code HResult=-2147024809 Message=Can not instantiate proxy of class: MongoDB.AspNet.Identity.UserStore`1[[MVC.Models.ApplicationUser, MVC,

How to mock rows in a Excel VSTO plugin?

巧了我就是萌 提交于 2019-12-04 23:40:52
I am trying to put a mocked Range (which contains cells with values) inside the rows of a new Range . But when I try to access a specific element from the Range , a exception is thrown. I've tried everything, does anyone have a idea what I am doing wrong here? Exception Message: Test method xxx.MockUtilsTest.MockRowsTest threw exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot apply indexing with [] to an expression of type 'Castle.Proxies.RangeProxy' Test [TestMethod] public void MockRowsTest() { var row1 = MockUtils.MockCells("test_row_1", "test_row_1"); var row2 =

Mock HostingEnvironment.QueueBackgroundWorkItem in xunit test

我的梦境 提交于 2019-12-04 23:15:52
I have a method using HostingEnvironment.QueueBackgroundWorkItem which I wish to unit test some behaviour before this call, however, the test is failing with System.InvalidOperationException : Operation is not valid due to the current state of the object. I suspect this I need to mock the HostingEnvironment but unaware of how to. To resolve this issue I defined an interface public interface ITaskScheduler { void QueueBackgroundWorkItem(Action<CancellationToken> workItem); } In production code I inject implementation public class AspNetTaskScheduler : ITaskScheduler { public void

How to write unit test for private method in c# using moq framework?

匆匆过客 提交于 2019-12-04 23:13:10
I want to write unit test for private method in C# using moq framework, I've search in StackOverFlow and Google, but I cannot find the expected result. Please help me if you can. You can't, at least not with Moq. But more importantly, you shouldn't . First off, you don't test methods , you test behaviours . Second, in order to test behaviours, you exercise a type's public API and verify the outcomes of that exercise. Private methods are implementation details. You don't want to verify how things get done, you want to verify that things do get done. Grinn Perhaps you shouldn't (see other

Accessing the original arguments of Expect() when assembling the value in Returns()

跟風遠走 提交于 2019-12-04 22:27:47
Is it possible to get access to the parameter used to make a call to a mocked expectation when assembling the Returns object? Here is a stub for the objects involved and, given that, I am trying to mock a Collection: Class CollectionValue { public Id { get; set; } } Class Collection { private List<CollectionValue> AllValues { get; set; } public List<CollectionValue> GetById(List<int> ids) { return AllValues.Where(v => ids.Contains(v.Id)); } } Given a test list of CollectionValues that will be used for the mocked object, how does one go about setting up an expectation that will handle every

Mock object returning a list of mocks with Moq

天大地大妈咪最大 提交于 2019-12-04 20:51:51
问题 I am trying to test the following code public void CleanUp() { List<ITask> tasks = _cleanupTaskFactory.GetTasks(); //Make sure each task has the task.Execute() method called on them } In my test I create a mocked implementation of _cleanupTaskFactory, and I want to stub the GetTasks() method to return a type: List<Mock<ITask>> ...but the compiler won't accept that as a return value. My goal is to ensure that each task returned has the .Execute() method called on it using the Verify() MoQ

Unit Testing Unit of Work and Generic Repository Pattern framework using Moq

点点圈 提交于 2019-12-04 20:13:38
I am unit testing a Service which is using the a Unit of Work and Generic Repository using Moq. The problem is that in the service class the _subsiteRepository is always null when I run the test in debug mode. The setup of the Service Class I am mocking private readonly IRepository<Subsite> _subsiteRepository; public PlatformService(IUnitOfWork<PlatformContext> unitOfWork) { _subsiteRepository = unitOfWork.GetRepository<Subsite>(); } and the method in this class that am testing. The problem is that _subsiteRepository is always null. The method does more than this but this is the relevant part.

Partial mocking of class with Moq

China☆狼群 提交于 2019-12-04 17:47:36
问题 I want to mock only the GetValue method of the following class, using Moq: public class MyClass { public virtual void MyMethod() { int value = GetValue(); Console.WriteLine("ORIGINAL MyMethod: " + value); } internal virtual int GetValue() { Console.WriteLine("ORIGINAL GetValue"); return 10; } } I already read a bit how this should work with Moq. The solution that I found online is to use the CallBase property, but that doesn't work for me. This is my test: [Test] public void TestMyClass() {

Testing EF async methods with sync methods with MOQ

痴心易碎 提交于 2019-12-04 17:22:53
问题 I have this method: public async Task DeleteUserAsync(Guid userId) { using (var context = this.contextFactory.Create()) { var user = await context.Users.FirstOrDefaultAsync(x => x.Id.Equals(userId)); if (user == null) { throw new Exception("User doesn't exist"); } context.Users.Remove(user); await context.SaveChangesAsync(); } } I want to test it out. So I create the test: [TestMethod] public async Task DeleteUsersSuccessfulCallTest() { // Arrange var id = Guid.NewGuid(); var user = new User(