简体   繁体   English

茉莉花测试-如何测试正在应用的课程?

[英]Jasmine Testing - How to test for a class being applied?

I am currently creating my first JS project and am trying Jasmine unit testing for the first time. 我当前正在创建我的第一个JS项目,并且是第一次尝试Jasmine单元测试。

In my code, I want to test the function in one of my classes which applies a css class to a button element. 在我的代码中,我想在我的一个类中测试该功能,该类将css类应用于按钮元素。 However I am having difficulty getting the Jasmine test to pass and I am not sure why. 但是我很难通过茉莉花测试,我不确定为什么。

The test fails due to the test not seeing the class '.btn-active' in the targeted button, which is telling me that the function is not running, however I am unsure as to how to achieve this. 由于测试未在目标按钮中看到类“ .btn-active”,因此测试失败,这表明该函数未运行,但是我不确定如何实现此功能。

JS Class: JS类:


class ModeSelection {
        constructor() {
                this.easyBtn = $('#easy-mode');
                this.mediumBtn = $('#medium-mode');
                this.hardBtn = $('#hard-mode');
        }
        //applies the active class depending upon the button selected
        easyMode() {
                this.easyBtn.on('click', () => {
                        this.easyBtn.addClass('btn-active');
                        this.mediumBtn.removeClass('btn-active');
                        this.hardBtn.removeClass('btn-active');

                        $('.card-med').addClass('remove');
                        $('.card-hard').addClass('remove');
                });
        }

Please note, I have set a fixture before each test which contains the html button that I am looking to apply the class too. 请注意,我在每次测试之前都设置了一个夹具,其中包含我也希望应用该类的html按钮。

Fixture: 夹具:

beforeEach(function() {
        setFixtures(`
<div class="mode-select row">
                        <div class="col-sm-12 mode-header">
                           <h4>Select your difficulty</h4> 
                        </div>

                        <div class="col-xs-12 col-sm-4">
                            <button id="easy-mode" class="btn btn-mode btn-easy">Easy</button>
                        </div>
                         <div class="col-xs-12 col-sm-4">
                            <button id="medium-mode" class="btn btn-mode btn-med">Medium</button>
                        </div>
                         <div class="col-xs-12 col-sm-4">
                            <button id="hard-mode" class="btn btn-mode btn-hard btn-active">Hard</button>
                        </div>
</div>
      `)
});

Jasmine Test: 茉莉花测试:


 describe('Check easy mode functions', function() {
        beforeEach(function() {
            this.mode = new ModeSelection();

        });

      it('btn-active to be applied when easy function activated', function() {

            var spy = spyOn(this.mode, 'easyMode').and.callThrough();


            expect($('#easy-mode')).toHaveClass('btn-active')
        });

 });


Apologies if this is fairly basic, however I am quite new to Jasmine and was just seeking the best way to get this test to work. 道歉,如果这是相当基本的,但是我对Jasmine还是陌生的,他只是在寻求使该测试正常工作的最佳方法。

Thank 谢谢

Sam 山姆

This should work if you include your fixtures befor it: 如果您在其中包含了灯具,这应该可以工作:

let mode;
describe('ModeSelection', () => { // tested class name
  beforeEach(() => {
    mode = new ModeSelection(); // setup
  });

  describe('call easyMode()', () => { // tested method name 
    it('should add btn-active class to #easy-mode element', () => {
      mode.easyMode(); // call tested method
      expect($('#easy-mode')).toHaveClass('btn-active')// assert it ran correctly
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM