moq

Mocking a WCF client, with a parameterised constructor for .net component called from COM

自作多情 提交于 2019-12-11 03:57:39
问题 I'm struggling to find a way forward here. I have a VB6 screen, which i need to call a .Net 'adapter' component, which calls out to a WCF service. The WCF service is under windows authentication as i want to detect the windows logon of the user calling the functionality. The service reference is in the .Net adapter. To get this to work i have had to add detail to a binding, specifying the security, which all works fine in the real world. My problem is unit testing this, and trying to mock the

Moq: Unable to cast

房东的猫 提交于 2019-12-11 03:54:28
问题 I have the following mocks: var MockHttpContext = new Mock<HttpContextBase>(); var MockPrincipal = new Mock<IPrincipal>(); MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object); The error occurs when testing this line: var user = (CustomPrincipal)httpContext.User; This is the error: Unable to cast object of type 'IPrincipalProxy5c6adb1b163840e192c47295b3c6d696' to type 'MyProject.Web.CustomPrincipal'. My CustomPrincipal implements the IPrincipal interface. So can anybody explain

How to Moq this view?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 03:30:21
问题 I have a view for which I'd like to mock the Show behaviour. Once the credentials have been entered, the [Connecter] button enables itself, and then the user can click. I wish I could reproduce this behaviour without having to show the view and actually really enter my credentials. The application is a WinForms MDI presented by the IApplicationPresenter . The IApplicationPresenter raises the ShowView to which the IApplicationView subscribed. Then, when the IApplicationView.Shown , the

Moq to Rhino - fake partial repository

心不动则不痛 提交于 2019-12-11 03:09:01
问题 I got this really cool Moq method that fakes out my GetService, looks like this private Mock<IGetService<TEntity>> FakeGetServiceFactory<TEntity>(List<TEntity> fakeList) where TEntity : class, IPrimaryKey, new() { var mockGet = new Mock<IGetService<TEntity>>(); mockGet.Setup(mock => mock.GetAll()).Returns(fakeList); mockGet.Setup(mock => mock.Get(It.IsAny<int>())).Returns((int i) => fakeList.Find(fake => fake.Id.ToString() == i.ToString())); mockGet.Setup(mock => mock.Get(It.IsAny<Expression

Using Moq and Mock objects - my list count is always 0 when it should not be

天大地大妈咪最大 提交于 2019-12-11 01:52:51
问题 I am not sure why my get list method is bringing back 0 records in my test but when I run my application it pulls back a list of 5 items. [TestMethod] public void TestHasListOfSurveys() { var mockRepository = new Mock<ISurveyListRepository>(); var mockModel = new List<SurveyList>(); string testDate = DateTime.Today.AddYears(-1).ToShortDateString(); mockRepository.Setup(x => x.GetSurveyList(testDate)).Returns(mockModel); var testClass = new SurveyListModel(mockRepository.Object); var testModel

How do Moq and NHibernate automatically create derived types?

佐手、 提交于 2019-12-11 00:31:36
问题 When using NHibernate, you define your entites with virtual methods, and NHibernate will create a proxy object that tracks changes to your object. In Moq, the framework will magically create a derived type from an interface or a base class. e.g. var mock = new Mock<IFoo>(); IFoo magicFoo = mock.Object; This is really cool. How do these frameworks do it? Do they use reflection, generics, some kind of dynamic compilation, or something else? I realize these are both open source projects, and I

MSTest with Moq - DAL setup

感情迁移 提交于 2019-12-10 23:46:04
问题 I'm new to Moq, and just started on a project that's already in development. I'm responsible for setting up unit testing. There's a custom class for the DatabaseFactory that uses EnterpriseLibrary and looks like this: public Database CreateCommonDatabase() { return CreateDatabaseInstance(string.Empty); } private static Database CreateDatabaseInstance(string foo) { var database = clientCode == string.Empty ? DatabaseFactory.CreateDatabase("COMMON") : new OracleDatabase(new ClientConnections()

Wrap Sub as Function for use in Lambda

帅比萌擦擦* 提交于 2019-12-10 20:51:09
问题 I have a problem with VB9 and Moq. I need to call a verify on a Sub. Like so: logger.Verify(Function(x) x.Log, Times.AtLeastOnce) And my logger looks like this: Public Interface ILogger Sub Log() End Interface But with VB this is not possible, because the Log method is a Sub, and thereby does not produce a value. I don't want to change the method to be a function. Whats the cleanest way of working around this limitation and is there any way to wrap the Sub as a Function like the below? logger

Nunit testing with Mock. Instance of Interface

做~自己de王妃 提交于 2019-12-10 18:55:22
问题 I have the following (simplified) code. public class Controller { private readonly IService _service; public Controller(IService service) { _service = service; } public async Task<IHttpActionResult> Create(MyObject object) { var result = _service.method(object); if (!result.Succeeded) { return this.GetErrorResult(object); } } } and SimpleInjector is used to inject the dependency between _service and its implementation class, like this: public static void Register(Container container) {

What is the FakeItEasy equivalent of the Moq VerifyNoOtherCalls() method

一个人想着一个人 提交于 2019-12-10 18:24:39
问题 I'm currently a Moq user and I'm researching other mocking frameworks. When unit testing I frequently call _mock.VerifyNoOtherCalls() so I can be certain there are no unexpected interactions beyond the ones that I have already verified. I've searched the FakeItEasy docs and cannot find the equivalent option in their framework. Can anyone suggest how I might do this? 回答1: Strict fakes FakeItEasy supports strict fakes (similar to strict mocks in Moq): var foo = A.Fake<IFoo>(x => x.Strict());