Skip to main content

Build your own expectation library.

In the world of impending deadlines, testing is a key a vital role with any project. When building software you can test many functions and processes in a matter of minutes depending on how you setup your tests and how well the code was written to be testable. A key element for that tests is what you expect. Javascript for example, there are many expectation frameworks out there. Jasmine, Chai, Mocha to name a few. Let's build a simple expectation without their input.
var expect = function(obj) {
  return {}
}
This will be our starting point for what we will expect. Very simply it is a function that will accept an argument obj and then it returns some object back that will be defined in more detail soon. You may be thinking one object? That doesn't seem quite right. For an expect statement to work shouldn't we have something like "expect(a, b, 'equal')". Yes, we could do something like that. However, for this example, we will attempt to make the code read like a sentence. For example "expect(a).to.beEqual(b)". For this to work, we will create a singleton class out of the object that gets returned. The first item we will need is what value that we are wanting to test. This can be our expectation.
var expect = function(obj) {
  return {
    "expectation": obj  
  }
}
Once we have that value we need to create an object "to". This is really just for readability and organization.
var expect = function(obj) {
  return {
    "expectation": obj,
    "to": {}  
  }
}
Lastly, we need to create a function with the "to" object called beEqual(val).
var expect = function(obj) {
  return {
    "expectation": obj,
    "to": {
      "beEqual": function(val) {}
    }  
  }
}
At this point, you can run your expect statement and no matter what you put into it, the statement will pass. Depending on your test framework and how you use it this can be a real problem. The issue on this one is that we have not written any code to cause a failure yet. However as a reminder when you write your tests make sure that they will actually fail for the right reason. Tests that always pass are not necessarily useful. Hopping off the soap box and back into beEqual. In this function, we want to do 2 main things. The first is to check if the value in beEqual(val) is equal to the obj in the expect statement. The second is what to do if it is or is not equal. If they are equal we want to return true. If the answer turns out to be false, we want to throw an error and probably return some kind of message about why it failed as well.
var expect = function(obj) {
  return {
    "expectation": obj,
    "to": {
      "beEqual": function(val) {
        if (obj === val) {
          return true;
        } else {
          throw "Expected: " + val + " Got: " + obj;
        }
      }
    }  
  }
}
Congratulations you now have a very simplistic expectation framework that you can add too and modify to your hearts desire. There are many more opportunities to explore in this area. If you are new to coding etc, I suggest try writing a few more matchers that you can expect. Some examples toBeGreater, toBeLess, or maybe add a "not" object in there somewhere. Where ever it takes you I hope it ends up with better more solid code.

Comments

Popular posts from this blog

JSON Schema testing with Postman

Postman is becoming quite the popular tool for accessing and testing REST api services. One of the cool features is that you can write some Javascript tests on your responses. Built into Postman is also the Tiny Validator v4 and Cheerio. Cheerio is a small core JQuery like implementation. The problem I have is that there is no way to import a schema file from a remote place or a file. This will be about how I get around that issue.Pre-request Script var uri='http://someUrl.com'; $.get(uri + '/someFolder/schema.json', function(schema) { postman.setEnvironmentVariable('schema', JSON.stringify(schema)); }); This Pre-request script is what saves the day. There are a couple of timing issues that are present due to async processing. If you were to put this in your test script your chances of it not completing in time are very high and you would get inconsistent results. If you are familiar with JQuery the $.get should look familiar as a ajax call that d…

Mock HTTP calls with Angular and Protractor

One of the benefits using AngularJS is also using some test tools like Protractor. Protractor is a tool that was made for Angular testing. Many times when testing you will need to mock some data so that you can see if your application responds in the correct manner. Let us assume that we have the following HTML file running our application.
<!doctype html> <html ng-app="demo"> <head> <title>Hello</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-mocks.js"></script> <script src="hello.js"></script> </head> <body> <div ng-controller="Hello"> <p>Hello <span id="gname">{{exampleResponse.name}}</span></p> </div> …

Remote API useful tricks for Docker on Mac OS X

Install SocatSocat allow you to forward ports to a variety of things. For Docker forwarding a port to the docker.sock file makes a quick way to get access to the Docker remote api. Docker also allow a way to open up that TCP port however on Mac it is temperamental and hard to usebrew install socat socat -d TCP-LISTEN:2375,reuseaddr,fork UNIX:/var/run/docker.sock & Create an IP aliasRunning Docker containers locally is a fairly common thing. Interconnectivity between containers works naturally. However, sometimes you may also want a container to connect back to the Docker Remote API. If you are constantly on the go changing networks you do not want to have to change that IP address every time. A great use case for this is running Jenkins and using docker slavessudo ifconfig lo0 alias 192.168.99.50 Remote API There are also some interesting endpoints you can use for informationcurl http://localhost:2375 curl http://localhost:2375/containers/json curl http://localhost:2375/image…