【问题标题】:How do I get this rspec test to pass?如何让这个 rspec 测试通过?
【发布时间】:2013-04-14 02:50:51
【问题描述】:

我终其一生都无法弄清楚为什么这些测试会失败。

当用户输入他们的电子邮件/密码并点击登录按钮时,他们将被重定向到他们的个人资料页面,该页面将他们的名字放在标题中并在页面上显示他们的名字。它还显示了指向其个人资料的链接和退出链接。当我在浏览器中完成这些步骤时,一切都在它应该在的地方,但是当 rspec 运行时它继续失败。

我发现真正奇怪的是,当我运行一个 user_page_spec 测试来测试相同的元素时,这些都通过了。

我认为它与控制器中的 click_button 部分或“redirect_to 用户”有关,但我们将不胜感激。

这里是测试-

通过 user_pages_spec.rb- 中的测试

describe "profile page" do
    let(:user) {  FactoryGirl.create(:user)  }
    before {  visit user_path(user)  }

    it {  should have_selector('h1',    text: user.firstName)  }
    it {  should have_selector('title', text: user.firstName)  }
end

authentication_pages_spec.rb 中的测试失败 - 需要'spec_helper'

describe "Authentication" do
    describe "sign in" do
    .
    .
    .
    describe "with valid information" do
        let(:user) {  FactoryGirl.create(:user)  }
        before do
            fill_in "Email",            with: user.email
            fill_in "Password",     with: user.password
            click_button "Log in"
        end

        it {  should have_selector('title', text:user.firstName)  }
        it {  should have_link('Profile', href: user_path(user))  }
        it {  should have_link('Sign out', href: signout_path)  }

        describe "followed by signout" do
            before {  click_link "Sign out"  }
            it {  should have_link('Home')  }
        end
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails testing rspec tdd


    【解决方案1】:

    是的。最简单的疏忽总是会引起最大的麻烦。

    这就是发生的事情。

    而不是使用以下-

    describe "page" do
      it "should have something" do
        page.should have_selector('')
      end
    end
    

    Rspec 让你定义一个主题 -

    subject {  page  }
    

    这使您可以将第一个代码块简化为以下-

    subject {  page  }
    describe "page" do
      it {  should have_selector('')  }
    end
    

    这允许您运行多个引用该页面的测试,而无需额外输入。

    我在最顶部省略了主题 { page },所以我的 it {} 块都不知道要引用什么。添加后,所有测试都顺利通过。

    希望这对将来的其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-19
      • 2015-06-17
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多