mock

Moq how to correctly mock set only properties

匿名 (未验证) 提交于 2019-12-03 01:19:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: What is the correct way for dealing with interfaces the expose set-only properties with Moq? Previously I've added the other accessor but this has bled into my domain too far with random throw new NotImplementedException() statements throughout. I just want to do something simple like: mock.VerifySet(view => view.SetOnlyValue, Times.Never()); But this yields a compile error of The property 'SetOnlyValue' has no getter 回答1: public class Xyz { public virtual string AA { set {} } } public class VerifySyntax { [ Fact ] public void

How to Mock the Internal Method of a class?

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a class which has a internal method and i want to mock the internal method . But i am unable to mock it i.e. it is not calling the mocked function but calling the original function. Is there any way to achieve this ? Edit:Actually i am a novice to the Moq. I have many classes and methods of the classes to test using the Moq. Many classes are internal , many have internal methods, many have not-virtual methods . And can not change the signature on the methods and classes. Can anyone please let me know how to go about testing

How to mock angular translate filter in unit tests for directives

匿名 (未验证) 提交于 2019-12-03 01:17:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In my directive templates, I need to use the angular translate filter as such: {{'Data Source' | translate}} Then in my unit test for this directive, I get the error: Unknown provider: translateFilterProvider I've tried injecting $filter and getting $translate by $translate = $filter('translate'); which doesn't solve the problem - this is really for testing the filter I can inject the module pascalprecht.translate , but that is heavy handed. How do I best mock the filter? 回答1: Below is a simple example of how you can mock the filter. var

The provider for the source IQueryable doesn't implement IAsyncQueryProvider

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have some codes like below, I want to write unit tests my method. But I'm stuck in async methods. Can you help me please ? public class Panel { public int Id { get; set; } [Required] public double Latitude { get; set; } public double Longitude { get; set; } [Required] public string Serial { get; set; } public string Brand { get; set; } } public class CrossSolarDbContext : DbContext { public CrossSolarDbContext() { } public CrossSolarDbContext(DbContextOptions<CrossSolarDbContext> options) : base(options) { } protected override void

Asp.Net Core unit testing async controller

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have this test: [Fact] public async void Can_Paginate() { //fake data var product1 = new Product { ProductId = 1, ProductName = "P1" }; var product2 = new Product { ProductId = 2, ProductName = "Product 2" }; var product3 = new Product { ProductId = 3, ProductName = "Product 3" }; var product4 = new Product { ProductId = 4, ProductName = "Product 4" }; var product5 = new Product { ProductId = 5, ProductName = "Product 5" }; var data = new List<Product> { product1, product2, product3, product4, product5 }.ToAsyncEnumerable(); var category1

How to mock an IFormFile for a unit/integration test in ASP.NET Core 1 MVC 6?

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to write tests for uploading of files in ASP.NET Core 1 but can't seem to find a nice way to mock/instanciate an object derived from IFormFile. Any suggestions on how to do this? Thanks. 回答1: Assuming you have a Controller like.. public class MyController : Controller { public Task<IActionResult> UploadSingle(IFormFile file) {...} } ...where the IFormFile.OpenReadStream() is accessed with the method under test. You can create a test using Moq mocking framework to simulate the stream data. [TestClass] public class IFormFileUnitTests {

Mocking Apache HTTPClient using Mockito

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm trying to mock Apache HttpClient Interface in order to mock one of its methods mentioned below to return a stubbed JSON object in response. HttpResponse response = defaultHttpClient . execute ( postRequest ); Could somebody be able to suggest how to achieve this with some sample code? Your help would be greatly appreciated. Thanks 回答1: Here is what I did to test my code using Mockito and Apache HttpBuilder: Class under test: import java . io . BufferedReader ; import java . io . IOException ; import javax . ws . rs . core .

how to mock a URL connection

匿名 (未验证) 提交于 2019-12-03 01:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Hi I have a method that takes an URL as an input and determines if it is reachable. Heres the code for that: public static boolean isUrlAccessible(final String urlToValidate) throws WAGNetworkException { URL url = null; HttpURLConnection huc = null; int responseCode = -1; try { url = new URL(urlToValidate); huc = (HttpURLConnection) url.openConnection(); huc.setRequestMethod("HEAD"); huc.connect(); responseCode = huc.getResponseCode(); } catch (final UnknownHostException e) { throw new WAGNetworkException(WAGConstants.INTERNET_CONNECTION

Why am I getting an Exception with the message “Invalid setup on a non-virtual (overridable in VB) member…”?

匿名 (未验证) 提交于 2019-12-03 01:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a unit test where I have to mock a non-virtual method that returns a bool type public class XmlCupboardAccess { public bool IsDataEntityInXmlCupboard(string dataId, out string nameInCupboard, out string refTypeInCupboard, string nameTemplate = null) { return IsDataEntityInXmlCupboard(_theDb, dataId, out nameInCupboard, out refTypeInCupboard, nameTemplate); } } So I have a mock object of XmlCupboardAccess class and I am trying to setup mock for this method in my test case as shown below [TestMethod] Public void Test() { private string

Use mock MongoDB server for unit test

匿名 (未验证) 提交于 2019-12-03 01:07:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have to implement nosetests for Python code using a MongoDB store. Is there any python library which permits me initializing a mock in-memory MongoDB server? I am using continuous integration. So, I want my tests to be independent of any MongoDB running server. Is there a way to mock mongoDM Server in memory to test the code independently of connecting to a Mongo server? Thanks in advance! 回答1: You could try: https://github.com/vmalloc/mongomock , which aims to be a small library for mocking pymongo collection objects for testing