Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Stub a Promise in PhantomJS/Sinon

I have a class in jquery that calls a service and returns a promise, which is then executed in my main area via a .done and Im trying to wrap that call in a another class I have that will make sure multiple calls are not made for the same ID. However I am finding this very very difficult to test as I can not accurcately get the promise working in phantomJS/Sinon. Heres what the area Im trying to test is

LOCKER.execute(diagRunId, function (unlock) {
  SERVICE_ACCESSOR.makeCall params)
   .done(function (data) {
     console.log("Success!");
     unlock();
   })
   .fail(function (jqXHR, textStatus, errorThrown) {
     console.log("Failed!");
     unlock();
   });
});

and In my test file I have my setup like so

var setup = function() {

    P.mock('service-accessor', function () {
        return {
            makeCall: sinon.stub().returns({})
        };
    });

    P.mock('locker', function () {
        var methods = {
            execute: function (lockId, wrapped) {
                console.log('locked - ' + lockId)
                wrapped()
            },
        unlock: sinon.stub()
        };
        return {
            execute: methods.execute,
            unlock: methods.unlock
        };
    });

    P.start();
}

with finally the test just calling the method

aui.suite('Locker Test', function () {

        aui.test('should lock and then unlock', testFile, {
            setup: setup,
            browser: function () {
                P.when('utils', 'service-accessor','locker').execute(
                    'test', function (SERVICE_ACCESSOR, LOCKER) {
                       
                        UTILS.makeCall("Identifier")
                        expect(LOCKER.unlock).to.have.been.called.once;
                        done();
                    }
                );
            },
            validate: function () {},
        });
});

The locker works and begins execution of the service call, but then the service call fails with

     Error: PhantomJS: `TypeError: 'undefined' is not a function (evaluating 'SERVICE_ACCESSOR.callService( params).done')` near
L2492>          }).fail(function (jqXHR, textStatus, errorThrown) {

From my understanding my mock should return a just a empty object when its being called, but then I dont understand a) Why its going to fail and b) Whats undefined? My assumption is that its because Im not returning three objects, but Im trying to get it to succeed first! How can I correctly stub/mock this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In the end I didn't make a promise or use a stub. I used the following function that would call the done and fail in my call instead.

function() {
                return { done: function(callback) {
                    if(window.makeParamountCallSuccess) {
                        callback({"data": "data"});
                        return {
                            fail: function(){}
                        }
                    } else {
                        return {
                            fail: function(failCallback){ failCallback("jqXHR", "textStatus", "errorThrown")}
                        }
                    }
                   }
                }
            }```
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda