2016-10-26 2 views
2

'TypeError:未解決の' resolve 'プロパティを読み取ることができません。何かご意見は?MochaでPromiseをテストする際にエラーが発生しました - 'TypeError:未定義の' resolve 'プロパティを読み取ることができません'

describe('render fn',() => { 
    it('should return a string',() => { 
     let filename = __filename, 
      content = 'Hello, World', 
      theme = './test.less'; 
     return file.render(filename, content, theme) 
      .then((css) => { 
       expect(css).to.be.a('string'); 
     }); 
    }); 
}); 

は、ここで私がテストしようとしているコードです:

render(filename, content, theme) { 
    return new Promise((resolve, reject) => { 
     let options = JSON.parse(JSON.stringify(this.config.renderOptions)); 

     if (theme) 
      options.paths.push(path.resolve(this.config.theme.path, theme)); 

     options.paths.push(path.resolve(this.config[filename].resolve.root)); 

     less.render(content, options, (e, output) => { 
      if (e != null) reject(e); 
      resolve(output.css); 
     }); 
    }); 
} 

私は今、長い時間のためにこれに取り組んできた、と私は任意の助けをいただければと思います。 ありがとう!

+1

'render'を使って' var path = require( 'path'); 'を実行しましたか? – tcooc

+0

'this.config [filename]'は未定義ですか?エラーのスタックトレースは、エラーの正確な場所を示します(行+オフセット)。 – robertklep

+0

はい、私は 'var path = require( 'path');を先頭につけています。以前はそれを含めないと申し訳ありません。スタックトレースは、エラーがテスト中のreturn文と同じ行にあることを示しています。これは、offset = 17で、paramsの開始部分です。 –

答えて

1

I keep getting the error 'TypeError: Cannot read property 'resolve' of undefined,' and I don't know why it's happening. Any thoughts?

問題は、この行にある:あなたがオブジェクトthis.config[filename]のプロパティ.resolveを読み取ろう

options.paths.push(path.resolve(this.config[filename].resolve.root)); 

。エラーメッセージは、オブジェクトがundefinedであることを示しています。私はthis.config[filename]undefinedですなぜあなたは、あなたの質問に記載されているコードから言うことができない

var myObject = undefined; 
console.log(myObject.resolve); //Cannot read property 'resolve' of undefined 

:あなたのコードはに等しいです。

+0

'this'は' render() 'が呼び出されたコンテキストを指し、' path.resolve() 'のコンテキストではありません。さもなければ、 'this.config'を参照すると(おそらく)既にエラーが発生しています。 – robertklep

+0

@rabbitcoよく答えました!ありがとうございました! –

+0

@robertklep:いいえ.Javascriptインタプリタは、関数の実行を(1)作成段階と(2)実行段階の2つの段階に分割します。作成段階では、インタープリタは関数の引数、変数、関数を作成します。 'path.resolve()'が 'options.paths.push()'に引数として渡されるとき、 'options.paths.push()'の実行前に 'path.resolve()'が実行されます。匿名の 'Promise'関数と' render() 'です。 – rabbitco

関連する問題