it:ad:jasmine:howto:write_tests

IT:AD:Jasmine:HowTo:Write Tests

Once installed and configured, much like - but with a different syntax - you create Test Suites, that contain Tests, that contain Assertions.

Test SuitesTestsAssertions

But instead of using * IT:AD:MSTest's syntax of [TestClass],[ClassInitialize] [TestMethod], and Assert.IsTrue(...), or * IT:AD:NUnit's syntax of [TestFixture],[TestFixtureSetUp], [Test],and Assert.IsTrue,

you use describe, it, and expect/toBe:

//Test Suite
describe("A suite", function() {

  //when entering and exiting suite:
   beforeAll(function() {...});
  afterAll(function() {...});

  //run before and after each test:
  beforeEach(function() {...});
  afterEach(function() {...});

  //Test #1
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });

  //Test #2
  it("contains another spec with an expectation", function() {
    expect(false).toBe(false);
  });

  //Test #3
  it("contains an inverse assertion", function() {
    expect(false).not.toBe(true);
  });
  
  it("contains various comparisons", function() {
    //compare with null:
    var foo = null;
    expect(foo).toBeUndefined();

    //compare using ===
    expect(false).not.toBe(true);
    
    //compare using ==
    foo = 1;
    expect(foo).toEqual(1);
    
    //match using Regex:
    foo = "foo bar";
    expect(foo).toMatch(/bar/);
    expect(foo).toMatch("bar");

    //match against 'undefined':
    expect(bar).toBeUndefined();


    //loose boolean compare:
    foo = 0;
    expect(foo).toBeFalsy();

    //less and greater than:
    expect(1).toBeLessThan(3);
    expect(3).toBeGreaterThan(1);
  });
});

Notice:

  • It's a fluent syntax (as opposed to Attribute based as it is in C#)
  • Both describe and it take an argument pair of string,function
  • expect can be negated by prefixing it with not
  • /home/skysigal/public_html/data/pages/it/ad/jasmine/howto/write_tests.txt
  • Last modified: 2023/11/04 01:46
  • by 127.0.0.1