capybara与rspec visting错误的控制器

时间:2013-10-01 18:51:08

标签: ruby-on-rails rspec capybara

我试图通过填充一些值来测试我的控制器。我已将控制器规范文件放在spec/feature下。但是我的访问没有正常工作,它正在访问错误的控制器。

我的控制器规格文件是

require 'spec_helper'

describe ApplicationsController, :type => :controller do
  render_views
  #the problem is - we need to test criminal record data block from saferent response
  #this guy edwin avila is test input provided by saferent
  describe "GET 'new'" , :type => :request do
    before do
    visit '/TestApplication'
    fill_in 'application[applicants_attributes][0][first_name]', :with => 'Rose'
    click_link_or_button 'sbmt'
    end
        it 'has 200 status code if logged in' do 
        expect(response.status).to eq(200)
    end 
  end
end

我的Gem文件是

source 'https://rubygems.org'

gem 'rails', '4.0.0'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 4.0.0'
  gem 'coffee-rails', '~> 4.0.0'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
  gem 'yui-compressor'
end

gem 'jquery-rails'
gem 'git'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'
gem 'passenger'

# Deploy with Capistrano
gem 'capistrano'
gem 'rvm-capistrano'

# To use debugger
# gem 'debugger'
gem "xpath", "~> 2.0.0" 
gem "formtastic"
gem 'simple_form', :git => 'git://github.com/plataformatec/simple_form.git'
gem "rentjuicer"
gem "capybara", :group => [:development, :test]
#gem "capybara", "~> 2.1.0"
gem "daemons"
gem "mongoid", github: "mongoid/mongoid"
#gem "delayed_job_mongoid" #upto rails 3
gem "bson_ext", '~> 1.5'
gem "whenever", :require => false
gem "bitly", "~> 0.8.0"
gem "mogreet"
gem 'wicked_pdf'
gem 'httparty'
gem 'activemodel'
gem 'actionpack','~> 4.0'
gem "carrierwave-mongoid", "~> 0.6.3"
gem 'mongoid-grid_fs', github: 'ahoward/mongoid-grid_fs'
#gem 'client_side_validations'
#gem 'client_side_validations-simple_form'
#gem "client_side_validations-mongoid", "~> 4.1.2"
gem 'twilio-ruby'
gem "rash", "~> 0.4.0"
gem "activeresource", "~> 4.0.0"
gem 'rb-readline', '~> 0.4.2'
gem 'googlecharts'
gem 'htmlentities'
gem 'rspec-rails' , "~> 2.0"

gem 'mongoid-rspec' 

gem 'crack'
gem 'simplecov'

#gem 'whoops_rails_logger' upto rails 3

当我尝试运行时

rspec spec/features/applications_controller_spec.rb

我得到了

Failures:

  1) GET 'new' has 200 status code if logged in
     Failure/Error: visit '/TestApplication'
     NoMethodError:
       undefined method `collect' for nil:NilClass
     # ./app/controllers/home_controller.rb:8:in `index'
     # ./spec/features/applications_controller_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.14064 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/applications_controller_spec.rb:12 # GET 'new' has 200 status code if logged in

Randomized with seed 32162

我的路线档案

mYAPP::Application.routes.draw do
match 'street/:id' => 'home#street', :as => :street, via: [:get,:post]
  match '/pdf' => 'homes#pdf', via: [:get,:post]
  match '/login' => 'homes#login', via: [:get,:post]
  match '/changing_f_gal' =>'homes#changing_f_gal' , via: [:get,:post]
  match '/saferent_test' => 'applications#newnew', via: [:get,:post]
  match '/saferent_result' => 'applications#newcreate' , via: [:get,:post]
  match '/showall' => 'applications#showall', via: [:get,:post]
  match '/visitor/create_visits' => 'visitor#create_visits' , :via => :get
  match '/visitor/init_visitor' => 'visitor#init_visitor' , :via => :get
  match 'TestApplication' => 'applications#create', :via => :get


  root :to => 'home#index'
end

1 个答案:

答案 0 :(得分:0)

Capybara的访问方法不适用于该文件路径(假设您正在尝试访问应用程序的根目录)。尝试像

这样的东西
visit root_path

我建议您查看http://jnicklas.github.com/capybara

上的文档