Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Features

MattiSG edited this page May 15, 2012 · 5 revisions

Tests are organized by feature. The tests for a particular feature of application of your application should live in one source file under APP/test/right/features/. For example:

class ShoppingCartFeature < Test::Right::Feature
  def test_adding_item
    with LoginWidget do |w|
      w.login
    end

    with ItemWidget do |w|
      w.add_an_item
    end

    with CartWidget do |w|
      assert {
        w.number_of_items == 1
    end
  end
end

with

To perform actions on a Widget, use with, passing it the class of the widget you want to work with:

class ShoppingCartFeature < Test::Right::Feature
  def test_adding_item
    with LoginWidget do |w|
      w.login
    end
  end

The with method will automatically wait for the widget to appear and attach an object to the widget on the page for you to perform actions on. The object is passed to the the block as the only parameter.

You can also use named widgets in a with block:

class ShoppingCartFeature < Test::Right::Feature
  def test_adding_item
    with ItemWidget["shoes"] do |w|
      w.add_to_cart
    end
  end

This will attach w to the particular instance of the ItemWidget named by "shoes".

wait_for

If you just want to wait for a widget to become present on a page, you can use wait_for:

class ShoppingCartFeature < Test::Right::Feature
  def test_adding_item
    with ItemWidget["shoes"] do |w|
      w.add_to_cart
    end

    wait_for CartItemWidget["shoes"]
  end

data

Hard-coded literal strings in your tests are a recipe for data dependency and fragility. Use the Data Factory whenever you need a unique identifier to avoid clashes between tests.

class AdminFeature < Test::Right::Feature
  def test_creating_item
    item_name = data[:item]
    with ItemAdminWidget do |w|
      w.create_item(item_name)
    end

    wait_for ItemWidget[item_name]
  end

assertions

Test success or not is assessed through assertions.

assert

Use the assert method and pass it a boolean-returning closure (block). To use properties from Widgets, you will need to access the value member from the property, whose return value is wrapped in a Test::Right::Value object.

class ShoppingCartFeature < Test::Right::Feature
  def test_adding_item
    with ItemWidget["shoes"] do |w|
      w.add_to_cart
    end
    
    with CartWidget do |cart|
      assert {    # closure
        cart.list_text    # let's assume this is a property of CartWidget that returns the .text of an element
            .value        # unwrap the value
            .include?     # String method
            "shoes"
        }
    end
  end

assert_equal

You could use the assert_equal method to automate the unwrapping.

However, please note the parameters order is important for this unwrapping to happen: assert_equal <expected> <actual>. Otherwise, you will get the following type of error:

Test::Right::AssertionFailedError -
Expected #<Test::Right::Value:0x10167e038 @body=#<Proc:0x0000000100569950@/…/lib/test/right/widget.rb:51>>
but got "expected value"
Clone this wiki locally