加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:39:39 所属栏目:百科 来源:网络整理
导读:我想做这样的事情: 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
我想做这样的事情:

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的作者不允许嵌套功能.也许是为了可读性或者可能不觉得它是需要的,或者可能没有想到它……如果你真的想要嵌套功能,你总是可以尝试实现它并提取请求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读