员工模型的自定义方法 -backbone.js 一个测试用例失败

面临 getExperience 方法的问题。不知道为什么我会得到这个,有人可以帮助我吗?错误:在员工模型中测试自定义方法 测试员工模型的 getExperience 方法 在执行 getExperience 方法后检查员工模型中的体验失败 预期 0 为 21。


<!-- Test Case-->


  it('Checking experience in Employee Model after getExperience method execution', function() {

            var current_date = new Date();

            var current_year= current_date.getFullYear();

            var calculated_exp= current_year-1999;

              employee.getExperience(employee.get('year_of_joining'));

              expect(employee.get('experience')).toBe(calculated_exp);

          });


var Employee = Backbone.Model.extend({

 //Write your code here

 getExperience: function(experience) {

       var year_of_joining=employee.get('year_of_joining');

        var current_date = new Date();

            var current_year= current_date.getFullYear();

            var calculated_exp= (current_year)-(year_of_joining);

            return calculated_exp;

    }

});

var   employee = new Employee({

        employee_id:1111,

        name:'Sarah Roe',

        year_of_joining:1999,

       address:'ABC Street',

        experience:0

    });

    alert("Experience of "+employee.get("name")+" is "+employee.getExperience(employee.get('year_of_joining'))+" years");;

 <!-- Hmtl -->

<html>    

<head>    

</head>    

<body>    

<div id="app"></div




<script src="lib/jquery/dist/jquery.js"></script>

<script src="lib/underscore/underscore.js"></script>

<script src="lib/backbone/backbone.js"></script>  

<script  type = "text/javascript"  src="index.js"></script>


</html> 


一只斗牛犬
浏览 147回答 3
3回答

慕尼黑5688855

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script><script src="https://code.jquery.com/jquery-2.1.3.min.js"></script><script>// JS with example of setting experience value being set third Example// JavaScriptvar Employee = Backbone.Model.extend({&nbsp; //Write your code here&nbsp; default: {&nbsp; &nbsp; employee_id: 1111,&nbsp; &nbsp; name: 'Sarah Roe',&nbsp; &nbsp; year_of_joining: 1999,&nbsp; &nbsp; address: 'ABC Street',&nbsp; &nbsp; experience: null&nbsp; },&nbsp; getExperience: function(year_of_joining) {&nbsp; &nbsp; var current_date = new Date();&nbsp; &nbsp; var current_year = current_date.getFullYear();&nbsp; &nbsp; var calculated_exp = (current_year) - (year_of_joining);&nbsp; &nbsp; this.set({&nbsp; &nbsp; &nbsp; experience: calculated_exp&nbsp; &nbsp; });&nbsp; &nbsp; return calculated_exp;&nbsp; }});var employee = new Employee({&nbsp; employee_id: 721,&nbsp; name: "Shrikrishna",&nbsp; year_of_joining: 1999,&nbsp; experience: 22});//please add properties and values of Employee model//employee.set({'experience': employee.get('year_of_joining')}) ;alert("Experience of " + employee.get("name") + " is " + employee.getExperience(employee.get('year_of_joining')) + " years");</script><div id="container">Loading...</div>

尚方宝剑之说

这是backbonejs中员工模型自定义方法的正确答案:var Employee = Backbone.Model.extend({&nbsp; //Write your code here&nbsp;&nbsp; getExperience: function(year_of_joining) {&nbsp; &nbsp; var current_date = new Date();&nbsp; &nbsp; var current_year = current_date.getFullYear();&nbsp; &nbsp; var calculated_exp = (current_year) - (year_of_joining);&nbsp; &nbsp; this.set({&nbsp; &nbsp; &nbsp; experience: calculated_exp&nbsp; &nbsp; });&nbsp; &nbsp; return calculated_exp;&nbsp; }});var employee = new Employee({&nbsp; &nbsp; &nbsp; employee_id:1111,&nbsp; &nbsp; &nbsp; &nbsp; name:'Sarah Roe',&nbsp; &nbsp; &nbsp; &nbsp; year_of_joining:1999,&nbsp; &nbsp; &nbsp; &nbsp; address:'ABC Street',&nbsp; &nbsp; &nbsp; &nbsp; experience:0,&nbsp; &nbsp;});&nbsp;alert("Experience of "+employee.get("name")+" is "+employee.getExperience(employee.get('year_of_joining'))+" years");&nbsp;&nbsp; <!-- Hmtl --><html>&nbsp; &nbsp;&nbsp;<head>&nbsp; &nbsp;&nbsp;</head>&nbsp; &nbsp;&nbsp;<body>&nbsp; &nbsp;&nbsp;<div id="app"></div><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.4/underscore-min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.1/backbone-min.js"></script>&nbsp;&nbsp;<script&nbsp; type = "text/javascript"&nbsp; src="index.js"></script></html>&nbsp;

温温酱

experience您从未为模型的属性设置新值。为了让您的测试通过,您应该使用:&nbsp;expect(employee.getExperience()).toBe(calculated_exp);请注意, getExperience 函数永远不会使用 experience 参数。或者替换:employee.getExperience(employee.get('year_of_joining'));和:employee.set('experience', employee.getExperience());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript