TDD

After doing setup of TDD environment, as I have mentioned in

First will see some commonly used keywords in Test Cases:

1) describe .. end (its a block to start a test case)
2) subject  (we use this key word to set the subject of a test case)
3) before  (it runs the code inside the block before each example)
4) visit (visit a page)
5) it ( it is the content of test case in which we use different keywords to test a single step.)

we use these following keyword in “it” block:
a) should (e.g: it { should have_content(‘Abc’) } )
b) should_not (e.g: it { should_not have_content(‘Abc’) } )
c) have_content (e.g: it { should_not have_content(‘Abc’) } )
d) have_title (e.g: it { should have_title(‘bcs’)} )
e) have_selector (e.g: it { should have_selector(‘h1’, text: ‘Abc’) } )
f) respond_to (e.g: it { should respond_to(:name) } This example implicitly uses the Ruby method respond_to?, which accepts a symbol and returns true if the object responds to the given method or attribute and false otherwise )
g) be_[method name] (e.g: it { should be_valid} )
h) should eq

6) specify
It is a method,It is just a synonym for it, and can be used when writing ‘it’ would sound unnatural.

7)let
It provides a convenient way to create local variables inside tests. The argument of let is a symbol, and it takes a block whose return value is assigned to a local variable with the symbol’s name. In other words,

  let(:found_user) { User.find_by(email: @user.email) }

creates a found_user variable whose value is equal to the result of find_by.

Lesson 1: Write failing test first then write the code to pass test.

Lesson 2: Ruby uses a question mark to indicate true/false boolean methods. The tests themselves rely on the boolean convention used by RSpec, such as “respond_to”, in ruby it would be like this:

@user.respond_to?(:name)

Lesson 3:
Another example of the RSpec boolean convention, whenever an object responds to a boolean method foo?, there is a corresponding test method called be_foo. e.g:
we can test the result of calling

@user.valid?

with

@user.should be_valid

Lesson 4:
Open the appropriate file located under the spec/request directory and add this line at top of the file:

 require 'spec_helper'

Lesson 5:
(a)
Every test case start with “describe” keyword, use one main describe block which will be according to our controller/model name, e.g: as we created the file “abc_pages” in our previous post “Steps for TDD setup and execution” so in this file we will create block as:

describe "Abc Page" do
.
.
[write test content]
.
.
end

(b)
then write test cases for each action of abc_page in this main describe block which will also start with “describe” keyword.

Examples:

spec/requests/abc_pages_spec.rb
  describe "Abc pages" do
  subject { page }

  describe "Home page" do
    before { visit root_path }

    it { should have_content('Abc') }
    it { should have_title('Home') }
    it { should_not have_title('ab') }
  end
end

 

Pending test case:

describe "User page" do
  describe User do
    pending "add some examples to (or delete) #{__FILE__}"
  end
end

———— in progress ———————

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s