Skip to content

Instantly share code, notes, and snippets.

@ryanswood
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save ryanswood/9679411 to your computer and use it in GitHub Desktop.

Select an option

Save ryanswood/9679411 to your computer and use it in GitHub Desktop.
Jasmine

#What is Jasmine?

My talk is about showing the basic similarities between Jasmine and Rpsec. The best way to learn a new concept is to relate to existing knowledge.

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM.

Ruby / Rspec

def hello_world
  "Hello World!"
end

describe "#hello_world" do
  it "should return hello world" do
    expect(hello_world).to eq("hello world")
  end
end

JS / Jasmine

function helloWorld() {
    return "Hello world!";
}

describe("Hello world", function() {
  it("says hello", function() {
      expect(helloWorld()).toEqual("Hello world!");
  });
});

Features of Jasmine Test The main container is called a suite. An example of a suite is the describe("Hello world"... see above. Inside the suite are one or many anonymous it() blocks.

Matchers Types of Matchers

  • toBeNull()

  • toBeDefined();

  • toEqual(y);

    To find the inverse all you need is to append .not after the expect.

Before and After Each Jasmine also includes beforeEach() and afterEach()

The cool difference- Spies In Jasmine, a spy does pretty much what it says: it lets you spy on pieces of your program (and in general, the pieces that aren't just variable checks).

For example:

var Person = function() {};

Person.prototype.helloSomeone = function(toGreet) {
    return this.sayHello() + " " + toGreet;
};

Person.prototype.sayHello = function() {
    return "Hello";
};

describe("Person", function() {
  it("calls the sayHello() function", function() {
      var fakePerson = new Person();
      spyOn(fakePerson, "sayHello");
      fakePerson.helloSomeone("world");
      expect(fakePerson.sayHello).toHaveBeenCalled();
  });
});
Go over this part. Show how it works. May not have time to write code.

Resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment