Railsチュートリアル備忘録 -テスト編-


テスト

ページが存在することの検証

  test "should get home" do
    get root_path
    assert_response :success
  end

タイトルの検証

  test "home title" do
    get root_path
    assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
  end

統合テスト

作成

$ rails g integration_test テスト名

検証

$ rails test:integration

特定のHTMLが存在するかどうか

assert_select "文章"

あるパスが存在するかどうか

assert_select "a[href=?]", パス
例:assert_select "a[href=?]", about_path

あるバスが特定の個数存在するか

assert_select "a[href=?]", パス,count:数字
例:assert_select "a[href=?]", root_path, count: 2
Code マッチするHTML
assert_select "div" <div>foobar</div>
assert_select "div", "foobar" <div>foobar</div>
assert_select "div.nav" <div class="nav">foobar</div>
assert_select "div#profile" <div id="profile">foobar</div>
assert_select "div[name=yo]" <div name="yo">hey</div>
assert_select "a[href=?]", '/', count: 1 <a href="/">foo</a>
assert_select "a[href=?]", '/', text: "foo" <a href="/">foo</a>

追加中...