代码之家  ›  专栏  ›  技术社区  ›  Nir

水豚2.0中的嵌套特性+

  •  5
  • Nir  · 技术社区  · 13 年前

    我想做这样的事情:

    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
    
    1 回复  |  直到 12 年前
        1
  •  8
  •   deivid    12 年前

    编辑(2014年1月23日): 嵌套功能从2.2.1版本开始提供。看见 here

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

    你不能。这就是宝石的守护者所说的

    我想你可以称之为限制。 feature 不能嵌套。 您可以使用 context 或 describe 相反,但我会 建议不要疯狂使用这些,它往往会让测试变得很漂亮 无法读取。

    在其他一些情况下,可能会有人认为这很方便,但在这个特定的情况下,你应该使用 scenario 而不是嵌套的 特色 .

    此外,如果您想保持一致并在任何地方使用新的DSL,请使用 background 而不是 before 和 given 而不是 let 。像这样:

    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 因为 脚本 只是的别名 它 你不能筑巢 它 任何一个

    或者,如果您觉得旧的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的作者不允许嵌套 特色 的。也许是为了可读性,也许是觉得不需要它,也许是没有考虑过它……如果你真的想嵌套功能,你可以尝试实现它并请求它。