moq

Using IoC container as a service locator for HttpHandler

落爺英雄遲暮 提交于 2019-12-11 07:28:45
问题 This question relates to my other post. Ok so after a bit more messing around I decided to do it this way. Which seems to work fine when I run it, although I'm getting the following error in NUnit: Could not load file or assembly 'Castle.Core, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) So not sure what is happening there???

How to make Entity Framework DbContext generic for dynamic connection string?

谁说胖子不能爱 提交于 2019-12-11 06:55:35
问题 This is my method in the business layer: public async task<model> GetMemberList(CancellationToken cancelToken, string connString){ try { await Task.Run(() => { using (var dbContext = DbContext.Create(connString)) { Code Goes Here.... } }, cancelToken); } catch { Throw New Exception(); } } we have different clients databases. we pass connString from mvc controller to business layer methods to initialize new instance of dbContext to connect relevant client database. You can see in my method we

Moq Concrete Class with Internal Constructor

孤人 提交于 2019-12-11 06:27:24
问题 I'm attempting to Moq a concrete class that has an internal constructor, i.e., in MyAssembly I have public class MyClass { internal MyClass(){} // other methods including factory instance method } then in my test in `TestAssembly' I have var mock = new Mock<MyClass>(); in MyAssembly I have added the following to AssemblyInfo.cs [assembly: InternalsVisibleTo("TestAssembly")] but even setting TestAssembly to be a friend of MyAssembly , Moq still throws the error Castle.DynamicProxy

Why doesn't moq setup all properties by default?

一曲冷凌霜 提交于 2019-12-11 06:09:48
问题 I understand that in Moq, you need to set up properties before you can assign values. My question is why doesn't Moq just allow you to set properties by default without having to "set" them up. 回答1: That is a question for the developer. Till then you can take a look at the Quickstart and see if it helps answer your question. mock.SetupAllProperties(); which stubs all properties on a mock. My guess would be that not all mocks would possess properties so making the framework attempt to look up

TDD: Number of Asserts, and what to actually assert? [closed]

旧城冷巷雨未停 提交于 2019-12-11 06:09:36
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . I am currently writing tests using TDD and I have come up against a few queries. Normally when writing unit tests, i always used to use 1 assert per unit tests as this is what is defined as good practice and its easy to see why your test is failing. In TDD, is it also good

Enumerating a Mocked Indexer Property Causes the Collection to Become Empty

末鹿安然 提交于 2019-12-11 04:53:06
问题 Okay, to reproduce, here is what you need public interface IWorkbookSet { IWorkbooks Workbooks { get; } } public interface IWorkbooks : IEnumerable { IWorkbook this[int index] { get; } IWorkbook this[string name] { get; } int Count { get; } } public interface IWorkbook { IWorksheets Worksheets { get; } } public interface IWorksheets : IEnumerable { IWorksheet this[int index] { get; } IWorksheet this[string name] { get; } int Count { get; } IWorksheet Add(); IWorksheet AddAfter(IWorksheet

How do you moq a class for unit test

試著忘記壹切 提交于 2019-12-11 04:47:35
问题 This question is posted as a follow up to How do you Mock an class for a unit test that has a return type but no input parameters Since asking the original question I have now created a Minimal, Complete and Verifiable Example which is used as the basis for this question. I have a controller (shown below) public class HomeController : Controller { private OrganisationLogic _organisationLogic; public HomeController(OrganisationLogic logic) { _organisationLogic = new OrganisationLogic(); }

ASP.Net Web API 2 Controller Unit Test Get Request Item Count

不羁岁月 提交于 2019-12-11 04:27:20
问题 I am trying to develop an Xunit test to establish if my Controller under test is returning the correct number of objects. The Controller's getAreas function is as follows: [HttpGet()] public IActionResult GetAreas() { _logger.LogTrace("AreasController.GetAreas called."); try { // Create an IEnumerable of Area objects by calling the repository. var areasFromRepo = _areaRepository.GetAreas(); var areas = _mapper.Map<IEnumerable<AreaDto>>(areasFromRepo); // Return a code 200 'OK' along with an

Unit Test MVC 5 Controller Create Action with Unit of Work and Repository

£可爱£侵袭症+ 提交于 2019-12-11 04:13:36
问题 I am trying to test a controller action that accepts a view model and creates a new entry. Here is the controller action: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(ContactViewModel viewModel) { if (!ModelState.IsValid) return View("Create", viewModel); _unitOfWork.Contacts.Add(Mapper.Map(viewModel, new Contact())); _unitOfWork.Complete(); return RedirectToAction("Index"); } And the unit test: [TestClass] public class ContactControllerTests { private ContactsController

Return a Mock from a Mocked method

跟風遠走 提交于 2019-12-11 04:13:14
问题 I'm not sure how I would do this. Given I have public interface IFactory<T> where T : new() { IWrapper<T> GetT(string s); } public interface IWrapper<out T> where T : new() { void Execute(Action<T> action); } When I do this public class MoqTest { public void test() { Mock<IWrapper<basicClass>> wrapperMock = new Mock<IWrapper<basicClass>>(); Mock<IFactory<basicClass>> factoryMock = new Mock<IFactory<basicClass>>() .Setup(p => p.GetT(It.IsAny<string>())) .Returns(wrapperMock.Object); } } I get