设备的rspec请求无法正常工作以获取current_user和响应正文

时间:2013-07-22 08:55:32

标签: ruby-on-rails rspec devise capybara cancan

我是铁轨上的红宝石新手。我使用rails 4并为我的应用程序设计。我使用devse cancan进行授权和验证。但我试着尝试rsepc请求。在访问user_session_path后,我的应用程序没有得到响应,我无法从设计中获取current_user。

我的代码: * /支持/ devise_support.rb

module ValidUserRequestHelper
    def sign_in_as_a_valid_user

      @user ||= FactoryGirl.create :user

      visit user_session_path
      fill_in "Email", :with => @user.email
      fill_in "Password", :with => @user.password
      click_button "Sign in"
    end
end

RSpec.configure do |config|
    config.include ValidUserRequestHelper, :type => :controller
    config.include ValidUserRequestHelper, :type => :request
end

在spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

Capybara.javascript_driver = :webkit


Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }


ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|

  config.include Devise::TestHelpers, :type => :controller
  config.include Devise::TestHelpers, :type => :view


  Capybara.javascript_driver = :webkit

  config.fixture_path = "#{::Rails.root}/spec/fixtures"


  config.use_transactional_fixtures = true
  config.include Capybara::DSL

  config.infer_base_class_for_anonymous_controllers = false

  config.order = "random"
end

此配置在我的控制器规范中运行,但是当我尝试请求或查看规范时,我无法获取current_user或登录后无法重定向到主页(user_session_path)

这是我的要求规范

require "spec_helper"

describe "contact_view", :js => true do
  before do
    sign_in_as_a_valid_user

  end
  it "test click", :driver => :webkit do
    visit "home/index"
    puts page.html

    click_button "button"
  end
end

运行此代码输出后,如上面的文字:

.<html><head></head><body></body></html>
F

Failures:

  1) contact_view test click
     Failure/Error: click_button "button"
     Capybara::ElementNotFound:
       Unable to find button "button"
     # ./spec/requests/contact_spec.rb:27:in `block (2 levels) in <top (required)>'

按钮元素在我的视图中已准备就绪,因为登录后user_session_path没有重定向,因此找不到按钮元素。

请帮帮我.. 我的代码在哪里错了?

更新 我的控制器正在运行

require "spec_helper"

describe HomeController do

  before do
    sign_in_as_a_valid_user
    sign_in @user
    @user_param = {name:'zainul',phone:'0897741195',user_id:@user.id}
    @param = {format:'json'}
    post :create, @user_param.merge(@param)
    @id = Contact.find(@user.id)
  end

  describe "GET #index" do
    it "render :index view" do
      get :index
      flash[:notice].should match "Welcome back #{@user.email}"
      response.status.should be(200)
    end
  end

  describe "POST #create" do
    it "test assignment post" do
      response.body.should == true.to_json
    end
  end

  describe "PUT #update" do

    it "put update record in controller" do
      put :update, @param.merge({id:@id})
      response.body.should == true.to_json
    end

  end

  describe "DELETE #destroy" do

    it "delete record in controller" do
      delete :destroy,@param.merge({id:@id})
      response.body.should == true.to_json
    end

  end

  describe "GET #show" do
    it "show record by id for controller show" do
      param_grid={rows:4,page:1,sidx:"id",sord:"asc",id:@id}
      get :show,@param.merge(param_grid)
      response.should be_success
      body = JSON.parse(response.body)
      body.should include('rows')
      rows = body['rows']
      rows.should have(1).items
    end
  end
end

0 个答案:

没有答案