我想监视一个函数,然后在函数完成/初始调用时执行回调。
以下内容有些简单,但显示了我需要完成的工作:
//send a spy to report on the soviet.GoldenEye method function
var james_bond = sinon.spy(soviet, "GoldenEye");
//tell M about the superWeapon getting fired via satellite phone
james_bond.callAfterExecution({
console.log("The function got called! Evacuate London!");
console.log(test.args);
});
Run Code Online (Sandbox Code Playgroud)
在锡农有可能这样做吗?如果他们解决了我的问题,也欢迎备用库:)
如何进行第3次测试以检查异常消息中是否存在cause1?我还列出了前两个有缺点的测试.首先是不检查消息,第二个需要很多样板代码.
public class CheckExceptionsWithMockitoTest {
@Test(expected = RuntimeException.class)
public void testExpectedException1() {
A a = new A();
a.doSomethingThatThrows();
}
@Test
public void testExpectedException2() {
A a = new A();
try {
a.doSomethingThatThrows();
fail("no exception thrown");
} catch (RuntimeException e) {
assertThat(e.getMessage(), org.hamcrest.Matchers.containsString("cause1"));
}
}
@Test
public void testExpectedException3() {
A a = new A();
A spyA = org.mockito.Mockito.spy(a);
// valid but doesnt work
// doThrow(new IllegalArgumentException()).when(spyA).doSomethingThatThrows();
// invalid but in the spirit of what i want
//chekThrow(RuntimeException.class,containsString("cause1")).when(spyA).doSomethingThatThrows();
}
}
Run Code Online (Sandbox Code Playgroud)
我在Mockito中找不到有效的东西,但有些东西看起来可能(在语法层面)和能力. …
我想测试是否window.location.assign正在调用,所以我尝试使用spyOn(window.location, 'assign');,但该方法不可覆盖.
我是否可以使用任何其他方法来监视无法覆盖的本机方法?
我有一个Jasmine测试由于spyOn没有执行而不断失败.
以下测试将自动失败:
it('simple test', function() {
spyOn(angular, 'element');
});
Run Code Online (Sandbox Code Playgroud)
错误是:
TypeError: 'undefined' is not an object (evaluating 'angular.element(handle.elem).off')
at /Users/geoff/Project/www/components/angular-mocks/angular-mocks.js:1946
at /Users/geoff/Project/www/components/angular-mocks/angular-mocks.js:1977
Run Code Online (Sandbox Code Playgroud)
这个错误似乎只发生在angular.element.spy荷兰国际集团的其他angular方法,如angular.copy与angular.forEach不扔这个错误.我正在使用Jasmine 2.0和Angular~1.3.任何有关解决这个问题的建议都将受到赞赏.
我需要在具有@Autowired Logger实现的类上运行一系列单元测试。实现的基本思想是:
@Mock Logger logger;
@InjectMocks
TestedClass tested;
Run Code Online (Sandbox Code Playgroud)
但我想保存日志输出功能。
Mockito lib是否允许使用@InjectMock注入对象?我看过@Spy批注的示例,但是当我尝试使用它时,我总是收到NullPointerException。我知道我总是可以直接使用反射,但是这样做的目的是避免使用此类代码。
我是 jasmine 的新手,我需要在这个框架中为 node.js 应用程序编写一些单元测试。我有一些问题,其中之一如下所述:
var sampleFunction = function(){
var loader = new Loader(params);
// rest of logic here
}
Run Code Online (Sandbox Code Playgroud)
我想为sampleFunction. 为此,我需要在Loader构造函数上创建 spy并检查此构造函数作为参数获取的内容以及它返回的对象类型。
任何想法如何做到这一点?我尝试创建 spy on ,Loader.prototype.constructor但这不是解决此问题的方法。
有一种方法public Content createChild(String path, String contentType, Map<String,Object> properties)我想嘲笑.
我想以这种方式模拟它,使用任何类型的参数调用该方法,因此when()无法工作,因为我必须告诉它该方法应该接收哪些参数才能实际模拟.
所以我想实际上对任何方法调用做出反应,独立于其给定的参数(使用间谍?),然后调用某种"回调"来返回一个Content对象,我想要从该方法的真实参数中一起构建.
我在Mockito中找不到支持此功能的特定API.
嘿,我正在尝试使用 sinon 编写单元测试,但是,我无法重置存根的 sinon 更改行为。首先我有
TypeError: Attempted to wrap getLastData which is already wrapped
经过一番研究,我发现我需要重置 sinon,但我发现 sinon.restore 不是函数错误。此外,QueryHelper.getLastData() 只返回一个承诺,该承诺通过返回一个实体来解决lastData
var assert = require('assert');
var sinon = require('sinon');
var proxyquire = require('proxyquire');
var ExchangeHandlerFactory = require('../handler.js');
var QueryHelper = require('../query-helper.js');
describe('BinanceHandler', function() {
var binanceHandler;
var config;
before(function() {
config = {
exchange: 'binance',
interval: '1h'
};
var ExchangeHandlerFactoryObj = proxyquire('../handler.js',
{"./config.js": config,
"./query-helper.js": QueryHelper});
binanceHandler = ExchangeHandlerFactoryObj.getExchangeHandler('binance', '1h')
});
afterEach(() => {
QueryHelper.getLastData.restore();
});
describe('#buildGetMarketTickerUrl()', function() {
it('binance handler …Run Code Online (Sandbox Code Playgroud) 我正在尝试模拟使用 Jest的promise版本fs.writeFile,并且没有调用被模拟的函数。
要测试的功能(createFile.js):
const { writeFile } = require("fs").promises;
const createNewFile = async () => {
await writeFile(`${__dirname}/newFile.txt`, "Test content");
};
module.exports = {
createNewFile,
};
Run Code Online (Sandbox Code Playgroud)
玩笑测试(createFile.test.js):
const fs = require("fs").promises;
const { createNewFile } = require("./createFile.js");
it("Calls writeFile", async () => {
const writeFileSpy = jest.spyOn(fs, "writeFile");
await createNewFile();
expect(writeFileSpy).toHaveBeenCalledTimes(1);
writeFileSpy.mockClear();
});
Run Code Online (Sandbox Code Playgroud)
我知道writeFile实际上正在调用它,因为我运行node -e "require(\"./createFile.js\").createNewFile()"并创建了文件。
依赖版本
-- 这是对createFile.test.js文件的另一次尝试--
const fs = require("fs");
const …Run Code Online (Sandbox Code Playgroud) 我使用的是 Spring 3.2.11.RELEASe、JUnit 4.12 和 Mockito 1.10.18。在我的 JUnit 测试中,如何创建 @Autowired spring 服务的间谍(不是模拟,间谍)?以下是服务的声明方式……
@Service("orderService")
public class OrderServiceImpl implements OrderService, InitializingBean
{
Run Code Online (Sandbox Code Playgroud)
这是我的 JUnit 测试的设置方式……
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class ProcessPDWorkerTest extends AbstractWorkerTest
{
…
@Autowired
protected OrderService m_orderSvc;
Run Code Online (Sandbox Code Playgroud)
和
final OrderService orderSvcSpy = Mockito.spy(getTargetObject(m_orderSvc));
…
ReflectionTestUtils.setField(workerObj, "m_orderSvc", orderSvcSpy);
Run Code Online (Sandbox Code Playgroud)
我有……
protected static <T> T getTargetObject(Object proxy)
{
if ((AopUtils.isJdkDynamicProxy(proxy)))
{
try
{
return (T) getTargetObject(((Advised) proxy).getTargetSource().getTarget());
}
catch (Exception e)
{
throw new RuntimeException("Failed to unproxy target.", e);
}
}
return …Run Code Online (Sandbox Code Playgroud)