ywen.in.coding

Everythng I do

100% Test Coverage Is the Starting Point

| Comments

Introduction

I am surprised when I read this post because I am one of Martin Fowler fans and still is. But I have to disagree large part of Martin’s view on this issue.

First, the part that I agree with him is that test coverage should not be a goal, and a high number doesn’t mean much. But from this fact, I have my own conclusions.

What is ideal test coverage number

In short, 100% test coverage number is not a goal, it is a requirement, a starting point for doing good tests. Below 100% is unacceptable.

Why? I will use some ruby code as my example

1
return true if condition?

with one test in rspec:

1
2
3
4
5
6
context "when the condition is true" do
  it "returns true" do
    subject.stub(:condition?).and_return true
    subject.method.should be_true
  end
end

When you run the test with coverage, the line has a 100% coverage number. But do you really test the method well? the answer is no because you also need to test when condition is false. So the “real” coverage here is actually like 50% roughly.

Now think about it, when the test coverage tool tells you that you are 100% covered, it is probably lying, you don’t. What about Martin’s claims of “high coverage” like 90%? how poor that coverage is if 100% cverage is not that convicing?

Test every method?

Do I absoutely test every each single method? The answer is no. Here is an example:

I love constructor injection pattern, so a lot of my code looks like this:

1
2
3
4
5
6
7
Class A
  attr_reader :dep1, :dep2
  private :dep1, :dep2
  def initialize(dep1, dep2)
    @dep1, @dep2 = dep1, dep2
  end
end

How would I test this code in seperation? Basically I can’t, and I don’t. So if I write this code and commit, my test coverage will go below 100%. Is that OK? No, absolutely not OK. Then what do I do wrong?

I have to step back and ask myself, why do I write this code? Do I really need it? The answer most likely is: yes I do. However, it is not useful on its own, I will need my dependencies in some of instance methds in that class. But I don’t need the dependencies until I actually work on the instance methods.

So what I do wrong is the sequence of my actions. I should have had written one test that will present an intent, or a uasge of an instance of this class, and I start to impelment the method that does it. When I implement the method, I may find out that I have to add such a constructor (or not, for what it worth). At that time, I add this constructor and it is covered by an execution of the test against the instance method. My coverage number is still 100%.

This example essentially is the principle of “Do not test private methods”.

validates_present_of

I would like to be off topic a little bit to discuss a view that some people hold: do not test your validations in Rails since you are tesing the framework.

Well, the truth is, you are not testing framework. When you add one line into your class like

1
validates_present_of :name

You add a business rule in it. The framework does not have this rule, it doesn’t know the :name is required in this model class. So you should be testing this line to make sure name is being required.

Think of an exetreme situation: Given that you don’t test this line, some developer accidently deletes the line. No tests would fail and the fault code goes to production. Then next thing you know, you eitehr raises a bunch of 500 error when user doesn’t fill in the name, or worse, some users register successfully without a name. You then may not be able to charge his card because no name associates with it. Would you still think this line is framework concern at that point?

If you write you test like this:

1
2
3
4
5
it "is required" do
  subject.name = nil
  subject.valid?
  subject.errors[:name].should include("is required")
end

Then you are testing the framework because the error message is generated by the framework, not by your code. So something like:

1
subject.should have(1).error_on(:name)

is good.

Some would argue it is so painful to write like 5 lines of code for each attribute being validated. Very true, but how about this in your test:

1
it_requires_attributes :name, :email

This looks easy, right? All you have to do is to add a macro behind the scene to exapand into some good tests. I am not advocating this macro itself, I am just saying there are a lot of ways to make your tests DRY.

In team environments

It is very difficult to configure a CI environment with less than 100% test coverage. The problem is this: Say your manager demands a 86% test coverage. Then one could just wait for another teammate commits and increase the test coverage to 86.1%, then he commits without test, drop the test coverage back to 86%. Mission accomplished.

So the only way that makes sense is to ensure to increase or maintain the test coverage with every commits (or every merge back to the main line if you use git). To do so, a lot of job must be done on the CI servers side to be able to:

  • Know the current thershold
  • Increase the thershold automatically when the test coverage goes up
  • Break the build when the test coverage is below the thershold

With a 100% code coverage, this is so much simpler, each commit either has 100% coverage, or it doesn’t. The CI servers just need to fail those who don’t.

Summary

I understand Martin’s position completely. He is addressing the problem where the test coverage number becomes only a goal, a symbol, not the real effort. But it doesn’t mean a CI server should not measure the test coverage. In my own practice, when we have this number and when we realize how bad it is, we tend to do a better job, pay more attention to the code we write. It is a very useful tool for a team in learning.

When a team becomes better, the build will not break because of the test coverage, then it doesn’t really matter if the CI server keeps measureing it. It will give people confidence, validate what they have been doing. Thus a good ending.

My Coding Standard for Rails Projects (Part 3)

| Comments

Unit Testing

Unit Testing is the central piece of a project for two reasons:

  • Tests direct how the production code is written.
  • Tests assure any given execution paths within a unit (method) output when a developer thinks they output

I am going to trying to describe how the tests direct how the code under test is written.

I have class which takes a hash called settings and use the settings to do something.

Like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SomeClass
  attr_reader :settings
  private :settings
  def initialize(settings)
    @settings = settings
  end

  def deliver!(mail)
    list = Redis::List.new settings[:redis_key_name], :marshal => true
    settings[:marshallable_converters].each do |setting|
      mail = MarshallableConverterSetting.new(setting).marshallable_class.marshallable(mail)
    end
    list << mail
  end
end

Now the problem is that sometimes settings will miss some keys. It may not have :redis_key_name, for example. In this case, I can assume that a default for the missing keys.

The first attempt is to do something like this in the initialize:

1
2
3
4
5
6
7
8
def initialize(settings)
  @settings = default_settings.merge(settings)
end

private
def default_settings
  {:redis_key_name => "some-name", :marshallable_converters => :some_default}
end

First, let me say although this is relatively small, it breaks something important about OO design: the logic for inserting the defaults doesn’t belong to this class logically, it should not be this class’s responsibility to consider this.

In the real world, I may not realize this at all, it is such a small and innocent adds. But what happned was, I wrote my tests first and tried to go with this path, then I found out how am I going to test this? I could not check the settings hash since it is private (and it should be). I could call deliver!, adding contexts to check when a setting is misisng, the default takes over, but that will make the tests so messy and irrelevant. Then I figured I couldn’t do this. I should seek other implementations. So the end result is like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class SomeClass
  attr_reader :settings
  private :settings

  # @api private
  def initialize(settings)
    @settings = Settings.new(settings)
  end

  # @api private
  def deliver!(mail)
    list = Redis::List.new settings.redis_key_name, :marshal => true
    settings.marshallable_converters.each do |setting|
      mail = MarshallableConverterSetting.new(setting).marshallable_class.marshallable(mail)
    end
    list << mail
  end
end

A new class Settings is being added for handling default values. Both the code and tests become much simpler. As a bonus, Settings class is later being reused by other classes as well.

Integration Tests

A few words about the tools.

I don’t like cucumber since day one. It is messy with global steps; it claims writing cucumber tests is doing BDD, well, it is not. It is doing integration testing, pure and simple.

Reardless, cucumber is widely used and something better than cuucmber coming out from cucumber, such as sinnach and turnip.

The great thing about cucumber is Gherkin, a great way communicating between Business Analyst (BA) / Product Manager (PM) / QA and developers.

Integration tests are essential to a project: unit testing should cover all the logic within a method, but there is no guarantee that calls between methods will work correctly. We cannot prove the correctness of a program in a reasonable time, but we can increase the chance of its being correctly written by using the integration tests.

When a developer starting on a new feature, he writes a feature with several scenarios (could be written by a BA), which of course won’t pass. And actively communicate with business people on if this is what they want. Getting some resonable insights on what the feature is about, his task then become clearly defined: implement something to make the feature goes green.

Same for bug fixes, first write a feature that will fail because of the bug in question. Then fix the bug and verify the feature passes. This approach will give developer a clear defined goal. And in the end, the developer can say with confidence: I fixed the bug because now this feature is green. This is also a great way to let the managements have faith in the development team as well.

My Coding Standard for Rails Projects (Part 2)

| Comments

Builders

In the part 1, the code snippet about the controller has a line

1
user = Builders::User.new(params).build

This implies that a Builder Pattern is used for creating a model, more precisely, creating an “Aggregate” in the Domain Driven Design terminology. The return value of a builder should always be the root object of the aggregate. Below is an example of a build method.

User Builder (builder.rb) download
1
2
3
4
5
6
7
8
9
10
11
module Builders
  class User < Builder
    def build
      BusinessModel::User.build(params).tap do |business_object|
        business_object.email_addresses = [EmailAddress.new(params).build]
        business_object.phones = [Phone.new(params).build]
        business_object.address = Address.new(params).build
      end
    end
  end
end

A DRY version of a typical builder in the form of an internal DSL could be:

1
2
3
4
5
6
7
module Builders
  class User
    build_for :user,
      :has_many_associations => [:email_addresses, :phones],
      :has_one_associations => [:address]
  end
end

A lof of Rails projects I have seen have building email_addresses and phones logic in the UsersController, which then becomes hard to unit test and impossible to reuse.

Another replacement to the builders is using hooks, such as after_create. This makes your code highly depends on a specific ORM implementation, making the one line user.create! becomes hard to understand, and slow the unit tests.

The User BusinessModel should be responsible building a user and only it. Below is a possible implementation:

BusinessModel Builder (business_model_builder.rb) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module BusinessModel
  class User
    class << self
      def build(params)
        self.new(valid_params(params)).tap do |object|
          raise ObjectInvalidError.new(object.errors) unless object.valid?
        end
      end

      private
      def valid_params(params)
        [:id, :first_name, :last_name].inject({}) do |attributes, key|
          attributes.merge!(params[key])
        end
      end
    end
  end
end

This, as usual, can be abstracted into a DSL:

1
2
3
4
5
6
7
8
9
10
11
12
module BusinessModel
  class User
    class << self
      private
      def valid_params
        ...
      end
    end

    have_builder_method
  end
end

Business Models

A business model, in Domain Driven Design term, is a Entity. A business model should be persisted, but the model itself knows nothing about how itself being persisted, it delegates such a task to its persistence class.

A business model, contains attributes and business-related calculations based on these atrributes.

A business model also contains the validations that can be called by user.valid?

Persistence Classes

In the controller code in the part 1:

1
Persistence::User.new(user).persist

A Persistence class takes the business model object as its only parameter in its constructor and the persists the object. The Persistence object knows to how to map a model object’s attributes into database columns, for example. An example of such a method could be like this:

Uer Persistence Class (persistence_create.rb) download
1
2
3
4
5
6
7
8
9
module Persistence
  class User
    def persist
      ActiveRecordStore::User.new(business_object).save
      #save part of attributes to redis
      RedisStore::User.new(business_object).save
    end
  end
end

Buidling a business model from the persistence layer might look like this:

Uer Persistence Class (persistence_load.rb) download
1
2
3
4
5
6
7
8
module Persistence
  class User
    def load
      user = ActiveRecordStore::User.new(business_object).load
      RedisStore::User.new(user).load
    end
  end
end

The ActiveRecordStore::User in the code could be smart enough to load a user based on what attributes in the business_object. For example:

ActiveRecordStore example (active_record_store_load.rb) download
1
2
3
4
5
6
7
8
module ActiveRecordStore
  class User < Store
    def load
      non_empty_attributes = HashHelper.new(physical_attributes).non_blanks
      Physical::User.all(non_empty_attributes)
    end
  end
end

The code bears some explainations: physical_attributes is a method to transform the business model object attributes into the table columns. This method is used by both save and load. The Physical::User is a child of ActiveRecord::Base that talks to the database directly.

Some custom methods might be needed, such as find_by_email_address. These kind of finders, though, can be easily standardized.

1
generate_has_many_association_finders :user, :association_name => :email_addresses

DSLs

All the above examples can often be DRYed up by some internal DSLs. But I feel that I must say that these DSLs should not be overused.

A sympton of overuse of DSL (and thus over abstract the logic) is that the DSL allows too much options. When you find out you have to handle more than 2 slightly different situations in one DSL implementation method, it is time to seperate the situations into 2 or more slightly different DSL macros. And of course these DSL macros implementations can share a large portation of logic by abstract the common code out.

My Coding Standard for Rails Projects (Part 1)

| Comments

The purpose of this post is to discuss what is I think good when coding a Rails project. Some of thoughts incorporate the internal communications with my collogues Miles Georgi and Jeshua Borges. To publicize my thoughts as a public blog port, I hope I could get more feedbacks and improve upon the comments

Layers

The Rails framework is a layered architecture, and when an application becomes significantly more complicated, the default layers of Rails are not enough. I would like to discuss the layers I want to see to go into a Rails application. Next I will discuss these layers one by one

Thin controllers

Everybody talks about thin controllers, yet with all the trams I have worked with, the controllers never be thin. Why not? I think the answer is that people never really know where these extra yet necessary code should be put into if not in a controller. I will try to explain how I want to avoid any extra code put into the controller. In my view, the only tasks belong to a controller are:

  • Authentications
  • Authorizations
  • Delegate the tasks to other layers
  • Setup objects for page rendering

Putting anything more than the above tasks is considered to be too much by my standard.

A typical controller create, show ad index actions may look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class UsersController
  def create
    user = Builders::User.new(params).build
    Persistence::User.new(user).persist
    respond_with Presenters::User.new(user)
  end

  def show
    user = Finders::User.find(params[:id])
    respond_with Presenters::User.new(user)
  end

  def index
    users = Finders::Users.all(params)
    respond_with Presenters::MinimalUserList.new(users)
  end
end

You may see, if several models use the same pattern as described above, we can DRY it up using some nice little DSL:

1
2
3
4
5
class UsersController
  create_action :user
  show_action :user
  index_action :user, :with_presenter => Presenters::MinimalUserList
end

Even better, the specs can be as simple as:

1
2
3
4
5
  desceribe UsersController do
    include_examples "standard create action", :model_name => :user
    include_examples "standard show action", :model_name => :user
    include_examples "standard index action", :model_name => :user, :with_presenter => Presenters::MinimalUserList
  end

You could always choose not to use these default actions generators, however, it will be more coding and probably less readable code as the result.

Views and Presenters

Let’s face it: the views will always have logic for any given complicated enough sites. There will always be ifs and elses in the view templates. We cannot avoid them.

But by using the Presenter pattern, we can greatly make view much less complicated. Let’s see an example (I use HAML):

1
2
3
- image_name = @user.confirmed? "confirmed.png" : "unconfirmed.png"
%img{:source => image_name, :alt => "confirmed status"}
...

The above example already has a condition statement. With a presenter, the view can be changed to:

1
%img{:source => @user.confirmed_status_image_name, :alt => "confirmed status"}

The confirmed_status_image_name method returns the image path will be used. Natually, such a method should not be in a business model class since it concerns the view and only the view. A presenter is the best place to put such logic. The same logic, when in a view, is hard to unit test, but when it is in its own method, it is super simple to write the tests.

A view template should have one and only one instance variable that passed from its associated controller action: a some sort of presenter instance. It should not have anything else. When a view requires some logic, this logic belongs to an instance method of that presenter class.

What about if the controller is rendering a JSON? A presenter can and should help with that. Again, JSON is just another representation of your resource, thus JSON should not be part of your business logic, it should be in a presenter. Some code snippet shows you how simple it could be:

1
2
3
4
5
6
7
8
9
10
module Presenters
  class User < ObjectForDisplay
    to_json_properties :name_with_first_last_in_front, :email_addresses, :phone, :status
    forward_methods :email_addresses, :status, :phone

    def name_with_first_last_in_front
      "#{object.first_name} #{object.last_name}"
    end
  end
end

The to_json_properties defines a baseline of what attributes will be included in the to_json returns. Each attribute is either forwarded from the business model object, or defined in the presenter class when it apprantely doesn’t belong to a business model.