sinon

Testing polymer 1.0 components with iron-ajax using wct

微笑、不失礼 提交于 2019-12-10 23:51:21
问题 I am having the hardest time mocking the server response to an iron-ajax component inside my custom component. Here is my code files. custom-component.html: <link rel="import" href="/iron-ajax/iron-ajax.html"> <link rel="import" href="/internal-component/internal-component.html"> <dom-module id="custom-component"> <template> <iron-ajax url="staticFile.json" auto handle-as="json" last-response={{ajaxResponse}}></iron-ajax> <template is="dom-repeat" items={{ajaxResponse}} sort="_sort" id=

Sinon spy function called but not tracked

偶尔善良 提交于 2019-12-10 21:48:38
问题 I am using Mocha and sinon to spy on a function call. The function is called correctly but the spy is not tracking it. Here is the module i am testing export default (() => { function test1(){ console.log('called second func'); return 5; } function callThis(){ console.log('called first func'); test1(); } return { test1, callThis }; })(); and here is the test import Common from './common'; describe('spy test', () => { var setSpy = sinon.spy(Common, 'test1'); Common.callThis(); var result =

Sinon Fake server not auto responding

*爱你&永不变心* 提交于 2019-12-10 20:23:39
问题 Hi I am testing a collection using fetch, when I call it there is no answer from the fake server only after calling server.response I get the desired result. Why is that ? My code beforeEach( function() { server = sinon.fakeServer.create(); server.autoRespond = true; colhedoraList = new ColhedoraList(); }); . . . var spy = sinon.spy(colhedoraList, 'parse'); server.respondWith("GET", "getColhedoraInfo", [200, {"Content-Type": "application/json"}, '[{"id":"1","talhaoAtual":1,"posicionamentos":[

Sinon's fake server is not responding

烂漫一生 提交于 2019-12-10 20:14:13
问题 There are a number of other questions asking about Sinon failing to respond, but they all seem to resolve with something mundane, like invalid response data or a toggled configuration option. My situation is as follows: In the main application (at /js/app/ ) requireJS is used to load website application modules. For testing (at /js/test ) requireJS is also used to load the same modules, but adds Mocha, Chai, and Sinon. This is the bootstrap of the test application: define( "testRunner", [

How to add Sinon to angular 2 testing?

此生再无相见时 提交于 2019-12-10 18:26:58
问题 I want to add Sinon for testing but I can't get it to run. I've installed sinon and karma-sinon as DevDependencies. added sinon to Frameworks in my karma config file. frameworks: ['jasmine', 'browserify','sinon'], and in my test file I'm trying to declare a spy let setTokenSpy = sinon.spy(sbHttpObiject, 'checkForToken'); and I'm getting TypeScript error: app/sdk/test/http.spec.ts(87,22): Error TS2304: Cannot find name 'sinon'. Any help much appreciated! Btw, i think jasmine has also spies but

Sinon Stub/Spy Using WithArgs Not Behaving As Expected

笑着哭i 提交于 2019-12-10 17:08:59
问题 When I specify withArgs for a sinon spy or stub, I expect the callCount to only count calls with those arguments. This doesn't seem to be happening, though. If I run the following: var mySpy = sinon.spy(); mySpy.withArgs("foo"); mySpy("bar"); expect(mySpy.callCount).to.be(0); I get "expected 1 to equal 0". Am I crazy, is this a bug, or is there another way to do this? 回答1: You have to add withArgs to the assertion, too, like so: var mySpy = sinon.spy(); mySpy.withArgs("foo"); mySpy("bar");

Using Sinon.js and preventing a call to my app server

柔情痞子 提交于 2019-12-10 17:04:58
问题 Simple enough question: I want us sinon.js to test a piece of javascript to ensure that it calls the $.ajax method while doing two things: I don't want to actually hit the server I want to mock up a response from the server so here's the JS: $.ajax url: "/tickets/id.json" dataType: 'json' .done (data) => HandlebarsTemplates["tickets/popup_title"] data and here's my test: describe 'PopupDisplayer', -> beforeEach -> loadFixtures 'popup_displayer' new PopupDisplayer @title_stub = sinon.stub(

Enzyme: How to test onSubmit function passed as prop?

帅比萌擦擦* 提交于 2019-12-10 15:57:10
问题 I am fairly new with enzyme. I have two components under test. form.jsx const LoginForm = ({ style, handleSubmit }) => { return ( <form onSubmit={handleSubmit}> <Button type='submit'> Login </Button> </form> ); }; LoginForm.propTypes = { handleSubmit: PropTypes.func.isRequired }; I am using this component in another component as follows: Component.jsx export default class Login extends Component { constructor(props) { super(props); this.onLogin = this.onLogin.bind(this); } onLogin(event) {

Any better way than setTimeout to wait for asnyc callbacks when testing JavaScript?

寵の児 提交于 2019-12-10 15:34:05
问题 Given this code: showForm = function (url) { return $.get(url, function (html) { $('body').append(); }); }; When using sinon.js , jQuery.mockjax.js and expect.js , I have the following passing test: it("showForm calls jQuery.append", function () { $.mockjax({ url: '/fake' }); var spy = sinon.spy($.fn, "append"); presenter.showForm('/fake'); setTimeout(function () { expect(spy.called).to.be.equal(true); }, 1000); }); Using the setTimeout function to wait on the callback from the asynchronous $

How to unit test a function which calls another that returns a promise?

天涯浪子 提交于 2019-12-10 14:45:55
问题 I have a node.js app using express 4 and this is my controller: var service = require('./category.service'); module.exports = { findAll: (request, response) => { service.findAll().then((categories) => { response.status(200).send(categories); }, (error) => { response.status(error.statusCode || 500).json(error); }); } }; It calls my service which returns a promise. Everything works but I am having trouble when trying to unit test it. Basically, I would like to make sure that based on what my