How To Build Rock Solid Ruby On Rails App With BDD

 

 

What Is BDD ?

Behavior-driven development is basically evolved from Test-driven development. It differs by written in different languages and it will improve the communication between tech and non-tech teams and shareholders because the test cases written used this BDD are so much simpler than you think so even the non tech people can easily able to understand it in a first glance. In both development approaches are only based on tests cases but in BDD, tests are more user-focused and also it will work based on the system’s behavior.

 

Let’s look the actual formula of BDD,

 

The formula is, ‘Given-When-Then’ BDD which referred as,

 

Given – scenario
When – action
Then – Output

 

This formula makes our work lot easier to write BDD test cases for our application.

 

BDD Tools:-

Cucumber:-

Cucumber is one of the test framework which supports BDD. In this framework, We are using simple and plain English for writing test cases. This framework basically built in Ruby but also we can able to use it in different technologies.

 

SpecFlow:-

SpecFlow is developed from the cucumber framework which is built under Ruby. And it is similar like Cucumber because this also proclaims the simple user-friendly approach and this framework mostly used in.Net Projects.

 

Related: 10 Useful Tips & Tricks for Ruby On Rails Developers

 

Benefits of using BDD:-

Check out some of the cool benefits of using BDD for writing your test cases,

 

  • Here you can define the ‘behavior’ instead of defining test
  • To easy to write test cases & time
  • Criteria of the client can be easily achieved.
  • Capable of reaching wider audience because even the non-technical persons can cable to understand.

 

Architecture Of BDD Test Case Flow:

 

BDD

 

 

Once your first scenario passes, then it will move to the next one, and the same process will apply for each scenario.

 

BDD Tools in Rails:-

BDD has the in-built testing libraries which can help us to generate a new application with default options. And  all these testing libraries are comes from Ruby. Also there are tools which makes BDD easier to apply. Other side, even in Rails we have many tools to help us to write automated test in some of other popular frameworks such as RSpec, Capybara, Cucumber. For the main testing library, we can use RSpec and Cucumber for writing high-level acceptance test. Now, will see the how to use these gems in rails application.

 

Contact form 7 Mailchimp extension by Renzo Johnson - Web Developer

 

RSpec :-

RSpec is a unit test framework, Tests are written in RSpec based on the ‘behavior’ of an application while being tested. RSpec does not focus on how the application works and but instead on how it behaves.

 

Installing RSpec:-

 

1. Add gem ‘rspec-rails’ like
group :development, :test do
gem ‘rspec-rails’
end

2. bundle install
3. Run the command rails generate rspec: install  it will create a folder of spec
4. Execute the rspec with bundle exec rspec  and it will run all the spec files

 

Example:

require "rails_helper"
RSpec.describe Article, :type => :model do
context "with 2 or more articles" do
it "sorts them in reverse order" do
article= Article.create!
commentOne = article.comments.create!(:body => "comment one")
commentTwo = article.comments.create!(:body => "comment two")
expect(article.reload.comments).to eq([commentTwo, commentOne])
end
end
end

 

Sending Push Notifications to Android Apps & Angular Apps Using Ruby on Rails

 

Cucumber: –

It will read the plain text description of your application, organized in scenarios. Here the code is developed using concrete code, and it will also interact with your application from the user’s standpoint.

 

Installing Cucumber:-

1. Add gem ‘cucumber-rails’ in your Gemfile
group: test do
gem ‘cucumber-rails’
end

2. bundle install
3. Run the command rails generate cucumber: install it will create a folder of spec
4. bundle exec cucumber and it will run all the spec files

 

Example:-

Feature: Hello Cucumber
Scenario: User clicks the link
Given I am on the homepage
When I click the provided link
Then I should see the link click confirmation

 

For each scenario, we need to provide the corresponding steps definitions in ruby code. Below example will hep you to understand,

 

 

Then(/^I should see the link click confirmation$/) do
expect(page).to have_content("confirm")
end

 

Was it helpful? Find more blogs on latest technologies, feed your self with more informative blogs on all latest technologies. Also you can avail the best in class and  web and mobile app development service from highly skilled developers. For more inquiries reach us via info@agiratech.com