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

gab6jxml  于 2022-11-10  发布在  其他
关注(0)|答案(3)|浏览(168)

面对问题的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);
          });

第一个

2sbarzqh

2sbarzqh1#

<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
// JavaScript
var Employee = Backbone.Model.extend({
  //Write your code here

  default: {
    employee_id: 1111,
    name: 'Sarah Roe',
    year_of_joining: 1999,
    address: 'ABC Street',
    experience: null
  },
  getExperience: function(year_of_joining) {
    var current_date = new Date();
    var current_year = current_date.getFullYear();
    var calculated_exp = (current_year) - (year_of_joining);

    this.set({
      experience: calculated_exp
    });
    return calculated_exp;
  }

});

var employee = new Employee({
  employee_id: 721,
  name: "Shrikrishna",
  year_of_joining: 1999,
  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>
sirbozc5

sirbozc52#

您永远不要为模型的experience属性设置新值。为了让您的测试通过,您应该用途:expect(employee.getExperience()).toBe(calculated_exp);
请注意,getExperience函数从不使用experience参数。
或替换:
employee.getExperience(employee.get('year_of_joining'));
与:
employee.set('experience', employee.getExperience());

yb3bgrhw

yb3bgrhw3#

以下是backbonejs中employee model的自定义方法的正确答案:
第一个

相关问题