moq

Unit test a method that sorts a collection

心已入冬 提交于 2019-12-11 13:39:44
问题 I have a method that sorts a collection based on a property like this: public List<Student> GetAllStudents() { return _studentCatalogContext.Student.Where(x => (x.Course != 2 && x.Course != 6)) .OrderByDescending(x => x.EnrollDateTime).ToList(); } So the idea is to have, in this case, the most recently enrolled Student first. Since the result of the method call will be a sorted list with the newest enrollment first I wrote a test like this: [TestMethod] public void Calling_GetAllStudents

Moq an interface property of a class

寵の児 提交于 2019-12-11 13:36:58
问题 Having the following: var contact = m_someFactory.Create<IContact>(contact, Page.ID); var organization = contact.Organization; I need to mock someFactory as well as Organization interface. I've tried this but contact comes back null. I want to set contact to whatever and contact.Organization to anything but not null IOrganization: var organizationMock = new Mock<IOrganization>(); ObjectFactoryMock.Setup(x => x.Create<IContact>()).Returns(new ContactFake{ Organization = organizationMock.Object

Mock My.User.IsInRole() error in Asp.Net MVC

Deadly 提交于 2019-12-11 12:22:52
问题 I have a unit test to mock My.User.IsInRole() used in the following controller, Public Class BookingController Public Function GetUserRole() As String If My.User.IsInRole("Agent") Then result = "Login as Agent" End If ``` End Function End Class trying to set up the mock in this test case (VB code): <TestMethod()> Public Sub Test() //Arrange 'Dim httpContext = New Mock(Of System.Web.HttpContextBase)() Dim principal = New Moq.Mock(Of IPrincipal)() 'httpContext.Setup(Function(x) x.User).Returns

Raising obfuscated events with moq throws error

拥有回忆 提交于 2019-12-11 11:46:41
问题 We have using Moq for two month now. However there is a problem which can not solve somehow. In visual studio all tests succeeded just fine. On the build server there are several tests which failed. What they have in common is, that they use the "raise" method to throw an event. Our build server tests obfuscated what is good to find obfuscation errors. Every "normal" expectation like "Setup(something).Returns(something)" works. Only the raise event fails. the stacktrace looks like the

Using moq to verify a method is called in a repository

大憨熊 提交于 2019-12-11 11:26:02
问题 I asked this question earlier about testing a controller action and verifying that a method in my repository was being called. The answer came back that I should be testing a Save method which is called inside the Register method (both in the same repository) in a seperate test on the repository only. That's what I thought, but I'm coming to do the test and I can't get it to work. :( Here's the repository test, where am I going wrong? [TestMethod] public void Register_calls_Save_method_when

How to Mock a list transformation using AutoMapper

谁都会走 提交于 2019-12-11 10:49:23
问题 I am using AutoMapper and have a definition of mapping engine as private readonly IMappingEngine _mappingEngine; I initialize it via constructor injection and use in the code as below var product=//Get a single product var productModel = _mappingEngine.Map<ProductModel>(product); The above works perfectly. I now need to map a list of Product to list of ProductModel The following works in the controller action var entities =//Get list of products var model = entities.Select(e => _mappingEngine

Why does my Moq IEventAggregator verification fail?

旧城冷巷雨未停 提交于 2019-12-11 09:26:34
问题 I use Composite WPF(Prism) and I am trying to unit test that my Controller does in fact subscribe to a Composite Event. My subscription code looks as follows... //Init Events. this.eventAggregator.GetEvent<PlantTreeNodeSelectedEvent>().Subscribe( ShowNodeDetails, ThreadOption.UIThread); My unit testing code looks as follows (I use Moq as my Mocking Framework and Unity as my DI Framework)... Mock<PlantTreeNodeSelectedEvent> eventBeingListenedTo = new Mock<PlantTreeNodeSelectedEvent>();

Mocking a dialog box that takes a callback type as argument

帅比萌擦擦* 提交于 2019-12-11 09:18:57
问题 I have created the mock setup for the dialog box of type: void ShowDialog(string windowName, string parentWindowName, Dictionary<string, object> inputFields, Action<Dictionary<string, object>> closeCallBack, Dictionary<string, object> windowProperties = null); like: UIServicemock.Setup(u => u.ShowDialog(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, object>>(), It.IsAny<Action<Dictionary<string, object>>>(), It.IsAny<Dictionary<string, object>>())).Callback(ViewName

Untitesting/Moq method calling webrequest

女生的网名这么多〃 提交于 2019-12-11 08:43:32
问题 I want to test a class that via a Webrequest sends texts, how can I using Moq mock the GetRequest methode, to test the GetRequest? public class Sender public void Send(string Message) { ... WebRequest myReq = GetRequest(sParmeter); } protected WebRequest GetRequest(string URl) { return WebRequest.Create(URl); } } 回答1: One of the approaches that I was gonna suggest is the same as the response by AD.Net, but if you do not wish to modify your existing structure and still test you could do

How to mock a ReadOnlyCollection<T> property using moq

狂风中的少年 提交于 2019-12-11 08:24:37
问题 I'm trying to mock this ReadOnlyCollection property: private readonly IList<MyClass> myList = new List<MyClass>(); public virtual ReadOnlyCollection<MyClass> MyList { get { return new ReadOnlyCollectionBuilder<MyClass>(this.myList).ToReadOnlyCollection(); } } using this mock (as seen here): IList<MyClass> mockList = GetElements(); mockObj.SetupGet<IEnumerable<MyClass>>(o => o.myList).Returns(mockList); However at runtime I get an InvalidCastException: Unable to cast object of type 'System