首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rspec / Capybara问题-- Capybara不会访问页面

Rspec / Capybara问题-- Capybara不会访问页面
EN

Stack Overflow用户
提问于 2014-06-29 22:07:37
回答 2查看 776关注 0票数 1

好吧,我是个新手,但对我在做什么有一定的概念,但这篇文章在过去的几个小时里把我难住了,所以任何帮助都是非常感谢的。我正在构建一个站点,并且一直在使用Rspec和Capybara来测试我的站点。我最终需要删除turbolinks来改进我的jscript的功能。下一次我尝试运行测试时,我的测试套件的50%就神奇地崩溃了。我已经将范围缩小到所有失败之间的共性是“访问”要么出现在代码块中,要么就出现在代码块之前。因此,基本上移除Turbolinks以某种方式炸毁了Capybara或Rspec。我真的很难弄清楚这个问题。我试着更新宝石,但不起作用。我猜下一步要么跳过我不想做的TDD概念,要么开始卸载gem并重新安装,并祈祷这不会让我的应用程序变得无用……任何人能提供的任何帮助都是非常感谢的,如果你在纽约市,我会请你喝一杯啤酒。

还有一些测试失败了,这些测试不需要任何类型的身份验证,只是为了检查页面的标题和内容,而那些测试失败了。我提出这个问题只是想说我不认为是FactoryGirl造成了这个问题。

干杯。

错误

代码语言:javascript
运行
复制
2) User pages profile page                                                                                                                                                                  
 Failure/Error: before { visit user_path(user) }                                                                                                                                          
 NoMethodError:                                                                                                                                                                           
   undefined method `user_path' for #<RSpec::ExampleGroups::UserPages::ProfilePage:0x00000004290410>                                                                                      
 # ./spec/requests/user_pages_spec.rb:10:in `block (3 levels) in <top (required)>'                                                                                                        

3) User pages profile page                                                                                                                                                                  
 Failure/Error: before { visit user_path(user) }                                                                                                                                          
 NoMethodError:                                                                                                                                                                           
   undefined method `user_path' for #<RSpec::ExampleGroups::UserPages::ProfilePage:0x00000004207c00>                                                                                      
 # ./spec/requests/user_pages_spec.rb:10:in `block (3 levels) in <top (required)>'                                                                                                        

4) User pages signup page                                                                                                                                                                   
 Failure/Error: before { visit signup_path }                                                                                                                                              
 NameError:                                                                                                                                                                               
   undefined local variable or method `signup_path' for #<RSpec::ExampleGroups::UserPages::SignupPage:0x000000041b3088>                                                                   
 # ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'

测试套件代码

代码语言:javascript
运行
复制
require 'spec_helper'

describe "User pages" do 

subject { page }

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

    it { should have_content(user.name) }
    it { should have_title(user.name) }
end 

#Signup page tests
describe "signup page" do 
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
end 

describe "signup" do 

    before { visit signup_path }

    let(:submit) { "Create my account" }

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

    describe "with valid information" do 
        before do
            fill_in "Name",                 with: "Example User" 
            fill_in "Email",                with: "user@example.com"
            fill_in "Password",             with: "foobar00"
            fill_in "Confirmation",         with: "foobar00" 
        end 

        it "should create a user" do 
            expect { click_button submit }.to change(User, :count).by(1)
                end
           end 
      end
end
EN

回答 2

Stack Overflow用户

发布于 2014-06-29 22:24:50

尝试从before区块中删除访问页面。

所以,你只需要:

代码语言:javascript
运行
复制
describe "signup page" do 
  feature "should have content 'Sign up' "
   visit signup_path  
   it { should have_content('Sign up') }
  end
  feature "should have full title 'Sign up' "
   visit signup_path  
   it { should have_title(full_title('Sign up')) }
  end
end 

您需要在每个describe块中这样做。

票数 1
EN

Stack Overflow用户

发布于 2014-06-30 07:20:22

好的,对于其他遇到这个问题的人,我做了更多的挖掘,对于Capybara来说,有时变得奇怪并不是什么罕见的事情。但是我通过将"config.include Rails.application.routes.url_helpers“添加到spec_helper.rb文件来修复路由错误并传递绿色参数。

代码语言:javascript
运行
复制
Rspec.configure do |config|
*
*
* 
config.include Rails.application.routes.url_helpers

结束

不幸的是,我无法提供任何洞察,为什么需要添加这一行,而以前并不需要它。我不得不把答案留给比我知识更多的人去回答。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24476799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档