response
calls, I've tried to call it with .call
and .apply
to set what this
should be, and a number of other things, but this
is always {}
inside the intent object
this
inside that object so I can test if they're called?
const handlers = {
'HelloWorldIntent' : function() {
//emit response directly
this.emit(':tell', 'Hello World!');
}
};
this
as planned - gotta do some reading as to why the arrows weren't working
// Import qbs
import {getQbo} from "./../../utils/quickBooks/qbo.module";
// My new instance
const invoiceModule: InvoiceModule = new InvoiceModule(getQbo);
// My class
export class InvoiceModule {
qbo: any;
addressModule: AddressModule = new AddressModule();
customerModule: CustomerModule = new CustomerModule();
emailModule: EmailModule = new EmailModule();
constructor(getQbo) {
const token = process.env.QB_APP_TOKEN;
const companyId = process.env.QB_COMPANY_ID;
const refresh = process.env.QB_APP_TOKEN_REFRESH;
this.qbo = getQbo(token, companyId, refresh);
}
}
@jeffhall-wk sorry for the wait. Hope it helps
before(marbles(function(m) {
composeInvoiceEmailHtmlMock = sinon.stub(InvoiceModule.prototype,'composeInvoiceEmailHtml')
.returns('<p>It was mocked</p>');*/
composeCustomerMock$ = sinon.stub(InvoiceModule.prototype, 'composeCustomerAndCreateCustomerInQBO')
.returns(m.cold('--a|', {a: customerMock}));
composeInvoiceMock$ = sinon.stub(InvoiceModule.prototype, 'composeInvoiceAndCreateInvoiceInQBO')
.returns(m.cold('--a|', {a: invoiceMock}));
sendEmailMock$ = sinon.stub(InvoiceModule.prototype, 'sendEmail$')
.returns(m.cold('--a|', {a: 'SENT_EMAIL'}));
}));
after(function () {
composeCustomerMock$.restore();
composeInvoiceMock$.restore();
sendEmailMock$.restore();
})
it('should create an invoice in QBO', marbles(function(m) {
const b = m.cold('------b', {b: 'SENT_EMAIL'});
m.expect(invoiceModule.createCustomerAndInvoiceInQBO$(rawDocument)).toBeObservable(b);
}));
it('should do something with stubs', sinon.test(function() {
var stub = this.stub($, 'post');
doSomething();
sinon.assert.calledOnce(stub);
});
var sinon = require('sinon');
let sinonTestFactory = require("sinon-test");
let sinonTest = sinonTestFactory(sinon);
it('should test the createECHeckCharge function', sinonTest(function() {
const that = this;
marbles(m => {
that.stub(request, 'post').yields(null, {OK: true}, {status: 'CAPTURED'});
m.expect(paymentModule.createCharge({})).toBeObservable(m.cold('a', {a: {status: 'CAPTURED'}}))
})()
}));
const callback = spy.stub(exampleFn())
, then does that mean the callback
will be able to do the job of my exampleFn
?
function wrapTypeError(typeError) {
const errData = {data: {event: 'TypeError'}};
return Object.assign(typeError, errData);
}
function wrapGotError(gotError) {
const apiClientError = apiClientErrorsFactory(gotError);
const error = { data: {}};
try {
Object.assign(error.data, apiClientError);
return error;
} catch (typeError) {
return wrapTypeError(typeError);
}
}
Object.assign
only when it's called with error.data
and apiClientError
so that it throws a TypeError
next
branch, and dist tags have not changed either: $ npm view sinon dist-tags
=>{ latest: '4.4.8', next: '5.0.0-next.3' }
Any people around here?
I need some help with using mocks. I want to mock a WebSocket to verify that my class registers a listener, but it seems sinon let's the original method execute? I was able to reproduce this in a simple test also:
class Thing {saySomething() { console.log("Something!");}}
const t = Object.create(Thing.prototype);
const mock = sinon.mock(t);
t.saySomething();
This actually logs 'Something!' to the console, I was under the impression that it shouldn't?
sandbox = sinon.sandbox
for default, but it also says sinon
is the default sandbox
Hi - I have a question about stubbing / doubling complex objects. I'm trying to mock an adonis model object - so here's the code I'm looking to test :
async index ({ request, session, view, response, params }) {
const messages = await Messages.query().has('messagedeliveryrecordsMessageId').with('messagedeliveryrecordsMessageId').fetch()
const messagesJson = messages.toJSON()
return view.render('messages/index', { title: 'Message Processing', messages: messagesJson })
}
at the moment I'm trying to create a mock of the message.query line :
const messages = await Messages.query().has('messagedeliveryrecordsMessageId').with('messagedeliveryrecordsMessageId').fetch()
I have a basic test like this - which works (as far as this line is concerned)
it.only('MessageController calls Messages.Query - returns null', async () => {
const messagesStub = sinon.stub()
messagesStub.toJSON = sinon.stub().returns('validJson')
const fetchstub = sinon.stub().returns(messagesStub)
const withstub = sinon.stub().returns({fetch: fetchstub})
const hasstub = sinon.stub().returns({with: withstub})
const mockMessages = sinon.createStubInstance(Messages)
mockMessages.query = sinon.stub().returns({has: hasstub})
mockMessages.toJSON = sinon.stub().returns('valid json here')
const SUT = new MessageController(mockMessages)
// Act
let result = await SUT.index({request:null})
// Assert
})
I plan on adding some asserts - like for instance asserting that the has stub was called with 'messagedeliveryrecordsMessageId' parameter.
My question is : All of the stub set up i'm doing in the test seems messy and not great. Is there a nicer way I can format this?
it.only('MessageController calls Messages.Query - returns null', async () => {
const mockMessages = {
query: sinon.stub().returns({
has: sinon.stub().returns({
with: sinon.stub().returns({
fetch: sinon.stub().returns({
toJSON: sinon.stub().returns('validJSON')
})
})
})
})
}
const SUT = new MessageController(mockMessages)
// Act
let result = await SUT.index({request: null, view: {render: sinon.stub()}})
// Assert
expect(mockMessages.query.calledOnce).to.equal(true)
expect(mockMessages.query().has.calledOnce).to.equal(true)
expect(mockMessages.query().has.getCall(0).args[0]).to.equal('messagedeliveryrecordsMessageId')
expect(mockMessages.query().has().with.calledOnce).to.equal(true)
expect(mockMessages.query().has().with.getCall(0).args[0]).to.equal('messagedeliveryrecordsMessageId')
})