5

私はこのようなことをしたい:

feature "sign-up" do
  before {visit signup_path}
  let(:submit) {"Create my account"}

  feature "with invalid information" do
    scenario "should not create a user" do
      expect {click_button submit}.not_to change(User, :count)
    end
  end

  feature "with valid information" do
    scenario "should create a user" do
      fill_in "Name",         with: "test name"
      fill_in "Email",        with: "test@test.com"
      fill_in "Password",     with: "password"
      fill_in "Confirmation", with: "password"
      expect {click_button submit}.to change(User, :count).by(1)
    end
  end
end

しかし、rspecを実行すると取得します

in `block in <top (required)>': undefined method `feature' for #<Class:0x000000039e0018> (NoMethodError)

このように少し変更すると、機能します。

  feature "with invalid information" do
    before {visit signup_path}
    let(:submit) {"Create my account"}

    scenario "should not create a user" do
      expect {click_button submit}.not_to change(User, :count)
    end
  end

  feature "with valid information" do
    before {visit signup_path}
    let(:submit) {"Create my account"}

    scenario "should create a user" do
      fill_in "Name",         with: "test name"
      fill_in "Email",        with: "test@test.com"
      fill_in "Password",     with: "nirnir"
      fill_in "Confirmation", with: "nirnir"
      expect {click_button submit}.to change(User, :count).by(1)
    end
  end

編集:

さらに、次のコードが機能します(ネストされた内部機能について説明します)-しかし、それは何らかの形で間違っていますか?

feature "sign-up" do
  background {visit signup_path}
  given(:submit) {"Create my account"}

  scenario "with invalid information" do
    expect {click_button submit}.not_to change(User, :count)
  end

  describe "with valid information" do
    background do
      fill_in "Name",         with: "test name"
      fill_in "Email",        with: "test@test.com"
      fill_in "Password",     with: "password"
      fill_in "Confirmation", with: "password"
    end

    scenario { expect {click_button submit}.to change(User, :count).by(1) }

    scenario "after submission" do 
      click_button submit
      page.html.should have_content("Registration successful")
    end
  end
end
4

1 に答える 1

8

編集 (2014 年 1 月 23 日):ネストされた機能はバージョン 2.2.1 以降で使用できます。こちらをご覧ください

編集 (2013 年 7 月 24 日):ネストされた機能は Capybara > 2.1.0 で許可されます。こちらをご覧ください

できません。これは、宝石の管理者がそれについて言っていることです

これは限界と言っていいのではないでしょうか。feature入れ子にすることはできません。contextどちらかまたは代わりに使用できますがdescribe、これらを使いすぎないことをお勧めします。テストがかなり読みにくくなる傾向があります。

他の場合では、これの便利さが議論されるかもしれませんが、この特定のケースでscenarioは、ネストされた の代わりに使用する必要がありますfeature

また、どこでも一貫して新しい DSL を使用したい場合は、background代わりにと をbefore使用してください。このような:givenlet

feature "sign-up" do
  background {visit signup_path}
  given(:submit) {"Create my account"}

  scenario "with invalid information" do
    expect {click_button submit}.not_to change(User, :count)
  end

  scenario "with valid information" do
    fill_in "Name",         with: "test name"
    fill_in "Email",        with: "test@test.com"
    fill_in "Password",     with: "password"
    fill_in "Confirmation", with: "password"
    expect {click_button submit}.to change(User, :count).by(1)
  end
end

は単なるエイリアスであるitため、削除する必要があり、どちらもネストできません。scenarioitit

または、以前の DSL の方が読みやすい場合は、いつでも元の DSL に戻すことができます。その場合、私は次のようなことをします:

describe "sign-up" do
  before {visit signup_path}
  let(:submit) {"Create my account"}

  context "with invalid information" do
    it "does not create a user" do
      expect {click_button submit}.not_to change(User, :count)
    end
  end

  context "with valid information" do
    before
      fill_in "Name",         with: "test name"
      fill_in "Email",        with: "test@test.com"
      fill_in "Password",     with: "password"
      fill_in "Confirmation", with: "password"
    end
    it "creates a user" do
      expect {click_button submit}.to change(User, :count).by(1)
    end
  end

end

しかし、仕様がチェックする必要があるものをチェックしている限り、とにかく問題ないはずです。残りはすべて、スタイル、読みやすさ、および優れた慣行の問題であり、これらは重要ですが、議論や意見の相違に対してよりオープンです。この場合、gem の作成者はネストされた を許可しませんでしたfeature。読みやすさのためかもしれませんし、必要だと感じなかったかもしれませんし、考えていなかったかもしれません...機能を本当にネストしたい場合は、いつでも実装してプルリクエストすることができます。

于 2013-03-06T17:11:15.467 に答える