微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails-3 – capybara 2.0中的嵌套功能

我想做这样的事情:

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: "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: "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

解决方法

编辑(2014年2月23日):从2.2.1版开始提供嵌套功能.见 here

编辑(2013年7月24日):Capybara将允许嵌套功能> 2.1.0.见here

你不能.这就是宝石的mantainer所说的

I guess you Could call this a limitation. feature can not be nested.
You can use either context or describe instead,but I would
suggest not going wild with these,it tends to make tests pretty
unreadable.

在其他一些情况下,可能会讨论这种方便,但在这个特定的情况下,您应该使用方案而不是嵌套功能.

此外,如果你想要一致并在任何地方使用新的DSL,使用背景而不是之前给予而不是让.像这样:

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

您必须删除它,因为方案只是它的别名,您也无法嵌套它.

或者,如果您发现它更具可读性,您可以随时切换回旧的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: "password"
    end
    it "creates a user" do
      expect {click_button submit}.to change(User,:count).by(1)
    end
  end

end

但只要规范检查它必须检查什么,你应该没事.其余的都是风格,可读性和良好实践,这些都很重要,但更容易讨论和分歧.在这种情况下,gem的作者不允许嵌套功能.也许是为了可读性或者可能不觉得它是需要的,或者可能没有想到它……如果你真的想要嵌套功能,你总是可以尝试实现它并提取请求.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐