Rspec has great feature and that is trait. In rspec we create factory for each class which provides the simplest set of attributes necessary to create an instance of that class. Many times we need some attributes which we do not want to add in original factory and at the same time we do not want to repeat it. In such scenario rspec trait is good option. For few attributes we need different values and we want to use it at multiple places can be another reason to use it.

Without Trait:

...

Read complete blog post

What do you do when you need to run the same test multiple times, but with different parameters? If you copy and paste the test, you end up with a hard-to-read test file. You can’t easily tell how the tests differ from one another. Worse, when you need to change one, you need to change them all. Take the following simple test file:

def double_it(number)
 number * 2
end

describe '#double_it' do
 it 'doubles 1 into 2' do
 expect(double_it(1)).to...

Read complete blog post

Recently I added few integration tests in my projects using Capybara and Selenium webdriver and ran into banging my head against inconsistencies with test database. I create some records in test DB which were completely invisible to Selenium-driven browser-based tests.

The problem is: the tests are being wrapped in database transactions, so any code running outside the actual test process (like, say, a server process servicing a Selenium-driven browser request) does not see the database...

Read complete blog post

Spec Improvements

We were fixing some failing specs in one of our project, we noticed beside failures the spec timings was too high. We spent time not only to fix also improve the performance. During this process we learned few good practices and removed bad things we added earlier which helped us improved the performance.

1) We had good amount of setup code for specs, for which we were using let statements. We can move the setup into a before(:all) block, replacing lets with instance...

Read complete blog post

What does Integration testing mean ?

Wiki says: Integration testing is the phase in software testing in which individual software modules are combined and tested as a group Read More

When a software grows(may it be written in Ruby or Java or any other language for that matter), stability can become an issue as the application evolve and grow. Integration tests provide a great way to perform end-to-end tests that validate the application is performing as expected.

The BDD-TDD life cycle goes...

Read complete blog post

What is mock ?
A mock is an object which we can use on behalf of another object.

In short, mocking is creating (fake) objects that simulate the behavior of Real Objects.

An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to test you replace the other objects by mocks that simulate the behavior of the real objects.

When to mock ?
Mocking is required when real objects are impractical to incorporate into the unit test. Most of...

Read complete blog post

Why write tests ?
Some answers might be Because I have been told by my manager Some could be yay! let's bolt on writing tests because it's cool

But some meaningful reasons could be
Test-Driven Development is a developer practice that involves writing tests before writing the code being tested. Begin by writing a very small test for code that does not yet exist. Run the test, and, naturally, it fails. Now write just enough code to make that test pass. No more.

It gives a strong base for your...

Read complete blog post

This post is to explain some insights of capybara-webkit and its functional setup.

What is capybara-webkit ?

Capybara-webkit is used for acceptance tests in our rails 3 application and it turned out to be non trivial, even though there are excellent tools out there, and they keep getting better.

WebKit is an open source web browser engine and capybara-webkit depends on a WebKit implementation from Qt, a cross-platform development toolkit.

Using capybara with the capybara-webkit driver, works...

Read complete blog post

Hello Everyone,

I wanted to share few links that Swapnil shared with me in the past. All of the links were really great. So I am sharing it here. Also if possible, just take an hour to go through the links. They are really good.

If you guys find any good links other than the ones I give below, please post them as comments.So that this post can help someone who needs good references for the specs in the future.

https://github.com/jnicklas/capybara

https://gist.github.com/zhengjia/428105...

Read complete blog post

What is fixture_builder ?

Fixture Builder creates fixtures based on your definitions of FactoryGirl

Why fixture_builder ?

In projects with huge test suite, its possible that the creation of AR objects hinders the test performance.
So to gain performance, we can avoid the redundant database transaction by building subsequent fixtures.

Examples

https://github.com/rdy/fixture_builder#example

It gives tremendous boost to your test execution time.

For us the statistics for 550 test cases were :

...

Read complete blog post

In rspec, we need to make http call for our test to pass. In this case, we are actually making http request which is not a best practice while we are executing tests.

The best way is to fake http request with the actual expected response without making actually going to that url.

For faking http request in ruby, we have couple of gems namely FakeWeb and WebMock.

FakeWeb:

Usage:

1) Include 'fakeweb' in your Gemfile under test group only.

2) require it in spec helper.

3) use it in specs

Syntax:...

Read complete blog post