2017-04-01 13 views
0

私はkarma-jasmine、webpactを使用して単体テストを実行しています。 ブックシェルフを使用してmysqlデータベース内にレコードを挿入しようとしました。オブジェクトに ReferenceError:本棚が定義されていません

にReferenceError:本棚がmodule.exports.addCategoryWithProductsで に定義されていない(2 app_serv /順序/試験/ order.util.spec.js:94215) Iは、次のエラーメッセージを受信しました。 ContextKarma.loaded(http://localhost:9876/context.js:151:12) TypeError:オブジェクトの未定義の のプロパティ 'then'を読み取ることができません。 (app_serv /順序/試験/ order.util.spec.js:94074:4)

例外TypeError:_this.driver.createConnectionではない機能

マイコード:

var knex = require('knex')({ 
    client: 'mysql', 
    connection: { 
    host  : 'localhost', 
    user  : 'user1', 
    password : 'toto', 
    database : 'totoDb', 
    charset : 'utf8' 
    } 
}); 

module.exports = require('bookshelf')(knex); 

module.exports.addCategoryWithProducts = function(categoryToAdd) { 

    bookshelf.transaction(function(t) { 
    var products = categoryToAdd.products; 
    var tableProd = []; 

    //if (!req.params.idCategory || req.params.idCategory ==0) { 
    Category.forge({ 
     name: categoryToAdd.name, 
     code: categoryToAdd.code, 
     tax_percentage: categoryToAdd.tax_percentage, 
     description: categoryToAdd.description 
    }) 
     .save(null, { transacting: t }) 
     .then(function(category) { 
     for (var i = 0; i < products.length; i++) { 
      tableProd.push({ 
      barcode: products[ i ].barcode, 
      cip_13: products[ i ].cip_13, 
      category_id: category.id, 
      quantity: products[ i ].quantity, 
      retail_price: products[ i ].retail_price, 
      description: products[ i ].description, 
      name: products[ i ].name, 
      inn: products[ i ].inn, 
      kind: products[ i ].kind, 
      comment: products[ i ].comment, 
      status: products[ i ].status, 
      critical_threshold: products[ i ].critical_threshold, 
      max_threshold: products[ i ].max_threshold, 
      date_end: products[ i ].date_end 
      }); 
     } 

     Products.forge(tableProd) 
      .invokeThen('save', null, { transacting: t }) 
      .then(function(products) { 


      //console.log('inside insert of collection '+ JSON.stringify({category : category.toJSON(), produits: products})) 
      //res.json({error : false, data: {category : category.toJSON(), products: products}}); 

      t.commit(); 

      return Promise.resolve(category.related('products')); 
      }) 
      .catch(function(err) { 
      return Promise.reject(err.message); 
      //console.log('Erreur dans Products.forge: ' + err.message); 
      res.status(500) 
       .json({ error: true, code: err.code, message: err.message }); 
      }) 

     }) 
     .catch(function(err) { 
     return Promise.reject(err.message) 
     //console.log('Erreur dans Products.forge: ' + err.message); 
     //res.status(500).json({error: true, code : err.code, message: err.message}); 
     }) 
    }); 
} 

ユニットテストCODE:

it("addCategoryWithProducts", function(done){ 
    addCategoryWithProducts(categoryMock) 
     .then(function(catWithPrds){ 
      expect(catWithPrds).toBeDefined(); 
      expect(catWithPrds.products).toBeDefined(); 
      expect(catWithPrds.products.length).toBe(3); 
     }) 

); 

失敗した多くのソリューションを試した後、私はあなたの助けを求めることにしました。

+0

あなたのコードが不完全に見えることができます。 'bookhelf'はどこにどのように定義されていますか?そして 'require( 'bookshelf')(knex);までのコードは、別のソースファイルに属しています。 – flaviodesousa

+0

はい、別のファイルにあります。 問題はそこにあるとは思わない。 – Evabila

+0

多分そうではありませんが、あなたのエラーは明示的に 'bookshelf'が定義されていないと言います。だから最初の誰もがどこにどのように定義されて見ていると思う。コードセグメントを正しく分割すると役立ちます。 – flaviodesousa

答えて

0

コードベースがどのように機能するかは完全にはわかりませんが、TypeError: Cannot read property 'then' of undefinedからユニットテストに約束を正しく返さないようです。

はまた、あなたはあなたの約束とトランザクションの処理を簡素化することができます

return Products.forge(tableProd) 
    .invokeThen('save', null, { transacting: t }) 
    .then(function(products) { 

Products.forge(tableProd) 
    .invokeThen('save', null, { transacting: t }) 
    .then(function(products) { 

を変更してみてください:

  • それが自動的にあるので、あなたは、明示的t.commit()を呼び出す必要はありません約束が解決するならBookshelfによって呼び出される
  • 通常、Promiseを別のPromise内で使用する必要はありません。だからではなく、return Promise.resolve(category.related('products'))のあなただけreturn category.related('products')、代わりのreturn Promise.reject(err.message)だけreturn err.message(それが再スローするように、おそらく良いでしょう)
+0

私は自分のコードを変更しようとしましたが、私はまだ同じエラーメッセージがあります。 あなたの提案に基づいて私のコードを簡素化してくれてありがとうございます。 – Evabila

+0

あなたはまだ 'TypeError:プロパティを読み取れません '、そして' undefined'エラーが出ていますか?それは私が思った唯一のものだったので、十分なデータがありました。 'ReferenceError:bookshelf is defined'と' TypeError:_this.driver.createConnection'のエラーは、 'bookshelf'リファレンスをどのように定義しているかに関係しているようです。 – flaviodesousa

関連する問題