it("toEqual", function () { let a = 12; expect(a).toEqual(12); });
toBeLessThan/toBeGreaterThan
1 2 3 4 5 6
it("toBeLessThan & toBeGreaterThan", function () { let pi = 3.1415926; let e = 2.78; expect(e).toBeLessThan(pi); expect(e).not.toBeGreaterThan(pi); });
toThrow/toThrowError
1 2 3 4 5 6 7 8 9 10
it("toThrow & toThrowError", function () { var foo = function () { thrownewTypeError("foo bar baz"); }; expect(foo).toThrow(); expect(foo).toThrowError("foo bar baz"); expect(foo).toThrowError(/bar/); expect(foo).toThrowError(TypeError); expect(foo).toThrowError(TypeError, "foo bar baz"); });
describe("A spec using beforeEach and afterEach", function () { var foo = 0; beforeEach(function () { foo += 1; }); afterEach(function () { foo = 0; }); it("is just a function, so it can contain any code", function () { expect(foo).toEqual(1); }); it("can have more than one expectation", function () { expect(foo).toEqual(1); expect(true).toEqual(true); }); });
beforeAll/afterAll
類似於beforeEach的功能,但beforeAll只會在整個describe中執行一次
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
describe("A spec using beforeAll and afterAll", function () { var foo = 0; beforeAll(function () { foo += 1; }); afterAll(function () { foo = 0; }); it("sets the initial value of foo before specs run", function () { expect(foo).toEqual(1); }); it("does not reset foo between specs", function () { expect(foo).toEqual(1); }); });
describe("A spy", function() { var foo, bar = null; beforeEach(function() { foo = { setBar: function(value) { bar = value; } }; spyOn(foo, 'setBar'); foo.setBar(123); foo.setBar(456, 'another param'); }); it("tracks that the spy was called", function() { expect(foo.setBar).toHaveBeenCalled(); }); it("tracks all the arguments of its calls", function() { expect(foo.setBar).toHaveBeenCalledWith(123); expect(foo.setBar).toHaveBeenCalledWith(456, 'another param'); }); it("stops all execution on a function", function() { expect(bar).toBeNull(); }); });
describe("A spy, when configured to fake a return value", function() { var foo, bar, fetchedBar; beforeEach(function() { foo = { setBar: function(value) { bar = value; }, getBar: function() { return bar; }, getBar1: function() { return bar; } }; spyOn(foo, "getBar").and.returnValue(745); spyOn(foo, "getBar1").and.callFake(function() { return1001; }); foo.setBar(123); fetchedBar = foo.getBar(); });
it("should not effect other functions", function() { expect(bar).toEqual(123); }); it("when called returns the requested value", function() { expect(fetchedBar).toEqual(745); }); it("when called returns the requested value", function() { expect(foo.getBar1()).toEqual(1001); }); });
it('two input', () => { let els = element.all(by.css('.input-small')); let first = els.get(0); // let first = els.first(); same as get(0) first.sendKeys('2'); let sec = els.get(1); // let sec = els.last(); same as get(1) sec.sendKeys('4'); });
it('calculate', () => { let els = element.all(by.css('.input-small')); let first = els.get(0); // let first = els.first(); same as get(0) first.sendKeys('2'); let sec = els.get(1); // let sec = els.last(); sec.sendKeys('4');