是否有一種方法來對一個Mongoose模型的虛擬屬性進行存根?對Mongoose模型的虛擬屬性進行存根
假設Problem
是一個模型類,並且difficulty
是虛擬屬性。 delete Problem.prototype.difficulty
返回false,並且該屬性仍然存在,所以我無法用我想要的任何值替換它。
我也試過
var p = new Problem();
delete p.difficulty;
p.difficulty = Problem.INT_EASY;
它沒有工作。
分配未定義Problem.prototype.difficulty
或使用sinon.stub(Problem.prototype, 'difficulty').returns(Problem.INT_EASY);
會拋出異常「類型錯誤:無法讀取財產‘範圍’的未定義」,而這樣做
var p = new Problem();
sinon.stub(p, 'difficulty').returns(Problem.INT_EASY);
將拋出一個錯誤「類型錯誤:試圖包裝字符串屬性難度作爲功能「。
我運行的想法。幫幫我!謝謝!
重寫'GET'作品,但與興農磕碰會拋出一個TypeError:無法讀取的未定義的屬性的構造函數「。所以我使用了第一個解決方案。謝謝! –
我發佈了一個適用於我的sinon的完整示例([email protected],[email protected],[email protected])。 –
我在您的示例的problemSchema中添加了'regionId',現在regionId不能在用sinon存根後分配。這就是爲什麼我得到的錯誤,當我嘗試斷言,'[「摩Silversword」,「艾因斯利場」] should.include(p.regionId);' –