【问题标题】:How to include script tag in teaspoon-Jasmine testing如何在 Teaspoon-Jasmine 测试中包含脚本标签
【发布时间】:2019-07-01 14:32:27
【问题描述】:

如何加载条带脚本,以便在我的 Teaspoon-Jasmine 测试期间定义条带。

错误:

失败/错误:ReferenceError:未定义条纹

茶匙测试:

describe("Stripe", function() {
  var paymentElement ;

  describe("constructor", function(){
    beforeAll(function(){
     // Tried this..

      var head = document.getElementsByTagName('head')[0];
      var jQueryScript = document.createElement('script');
      jQueryScript.setAttribute('type', 'text/javascript');
      jQueryScript.setAttribute('src', 'https://js.stripe.com/v3/');
      head.appendChild(jQueryScript);

     // also tried..

      $.getScript( "https://js.stripe.com/v3/");

      paymentElement = new Helpers.Stripe.PaymentElement(); 
    });

    describe("with defaults", function(){
     it("should define stripe", function(){
        expect(Stripe('test-token')).toBeDefined();
      });

      it("should define stripe through instance", function(){
        expect(paymentElement.stripe).toBeDefined();
      });

    });
  });
});

【问题讨论】:

  • 更正:love = load

标签: javascript jasmine teaspoon


【解决方案1】:

getScript 运行之后但在脚本被加载并且页面上存在Stripe 对象之前可能有一段异步时间。

Mocha 支持asynchronous callbacks,所以试试看,像这样:

  describe("constructor", function() {
    before(function(done) {
      $.getScript('script url here', function() {
        done();
      });
    });
  });

最近版本的 Mocha 支持直接返回 Promise,所以你也可以这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2011-12-12
    相关资源
    最近更新 更多