mock

How to android unit test and mock a static method

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Hi I really hope you can help me, I feel like I've been pulling my hair out for days. I'm trying to write unit tests for a method A. Method A calls a static method B. I want to mock static method B. I know this has been asked before, but I feel Android has matured since then, and there must be a way to do such a simple task without re-writing the methods I want to test. Here is an example, first the method I want to test: public String getUserName(Context context, HelperUtils helper) { if(helper == null){ helper = new HelperUtils(); } int

How do I mock a private field?

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm really new to mocks and am trying to replace a private field with a mock object. Currently the instance of the private field is created in the constructor. My code looks like... public class Cache { private ISnapshot _lastest_snapshot ; public ISnapshot LatestSnapshot { get { return this . _lastest_snapshot ; } private set { this . _latest_snapshot = value ; } } public Cache () { this . LatestSnapshot = new Snapshot (); } public void Freeze ( IUpdates Updates ) { ISnapshot _next = this . LastestSnapshot . CreateNext (); _next .

RhinoMocks: Correct way to mock property getter

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm new to RhinoMocks, and trying to get a grasp on the syntax in addition to what is happening under the hood. I have a user object, we'll call it User, which has a property called IsAdministrator. The value for IsAdministrator is evaluated via another class that checks the User's security permissions, and returns either true or false based on those permissions. I'm trying to mock this User class, and fake the return value for IsAdministrator in order to isolate some Unit Tests. This is what I'm doing so far: public void

How can I mock Webpack's require.context in Jest?

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Suppose I have the following module: var modulesReq = require.context('.', false, /\.js$/); modulesReq.keys().forEach(function(module) { modulesReq(module); }); Jest complains because it doesn't know about require.context : FAIL /foo/bar.spec.js (0s) ● Runtime Error - TypeError: require.context is not a function How can I mock it? I tried using setupTestFrameworkScriptFile Jest configuration but the tests can't see any changes that I've made in require . 回答1: Extract the call to a separate module: // src/js/lib/bundle-loader.js /* istanbul

Service mocked with Jest causes “The module factory of jest.mock() is not allowed to reference any out-of-scope variables” error

匿名 (未验证) 提交于 2019-12-03 02:52:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to mock a call to a service but I'm struggeling with the following message: The module factory of jest.mock() is not allowed to reference any out-of-scope variables . I'm using babel with ES6 syntax, jest and enzyme. I have a simple component called Vocabulary which gets a list of VocabularyEntry -Objects from a vocabularyService and renders it. import React from 'react'; import vocabularyService from '../services/vocabularyService'; export default class Vocabulary extends React.Component { render() { let rows = vocabularyService

Mock HttpResponse with Robolectric

匿名 (未验证) 提交于 2019-12-03 02:51:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Using Robolectric 2.3-SNAPSHOT, I want to test an object that'll execute a request in the background. In order to isolate it, I'm trying to mock the HttpResponse returned, without success after some hours invested. I've created a project that anyone can clone. Simly run ./gradlew check https://github.com/Maragues/RobolectricDummyProject (git clone https://github.com/Maragues/RobolectricDummyProject.git ) I've tried Robolectric.setDefaultHttpResponse(200, "my_mocked_word"); MockWebServer ( https://code.google.com/p/mockwebserver/ ) But the

How to mock test a web service in PHPUnit across multiple tests?

匿名 (未验证) 提交于 2019-12-03 02:51:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am attempting to test a web service interface class using PHPUnit. Basically, this class makes calls to a SoapClient object. I am attempting to test this class in PHPUnit using the getMockFromWsdl method described here: http://www.phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubbing-and-mocking-web-services However, since I want to test multiple methods from this same class, every time I setup the object, I also have to setup the mock WSDL SoapClient object. This is causing a fatal error to be thrown: Fatal error: Cannot

how to avoid returning mocks from a mocked object list

匿名 (未验证) 提交于 2019-12-03 02:51:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying out mock/responsibility driven design. I seem to have problems to avoid returning mocks from mocks in the case of objects that need a service to retrieve other objects. An example could be an object that checks whether the bills from last month are paid. It needs a service that retrieves a list of bills. So I need to mock that billRetrievalService in my tests. At the same time I need that BillRetrievalMock to return mocked Bills (since I don't want my test to rely on the correctness of the Bill implementation). Is my design flawed

In Python, what's a good pattern for disabling certain code during unit tests?

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In general I want to disable as little code as possible, and I want it to be explicit: I don't want the code being tested to decide whether it's a test or not, I want the test to tell that code "hey, BTW, I'm running a unit test, can you please not make your call to solr, instead can you please stick what you would send to solr in this spot so I can check it". I have my ideas but I don't like any of them, I am hoping that there's a good pythonic way to do this. 回答1: Use Michael Foord's Mock in your unit test do this: from mock import Mock

Mock static property with moq

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am pretty new to use moq . I am into creating some unit test case to HttpModule and everything works fine until I hit a static property as follows this . applicationPath = ( HttpRuntime . AppDomainAppVirtualPath . Length > 1 ) ? HttpRuntime . AppDomainAppVirtualPath : String . Empty ; I do not know how create mocks for static class and property like HttpRuntime.AppDomainAppVirtualPath . The context , request and response have been mocked well with sample code I get from moq. I will appreciate if somebody can help me on this. 回答1: