2016-12-27 96 views
0

我的规格/控制器/ undertakings_controller_spec.rb低于。Rspec和水豚,访问和获取之间有什么区别

RSpec.describe UndertakingsController, type: :controller do 
    describe 'redirect with home due to login user' do 
     subject {current_path} 
     it 'can get with authenticate undertaking user' do 
     login_user @undertaking.user 
     #get :show , id: @undertaking 
     visit undertaking_path(@undertaking) 
     expect(response).to redirect_to root_path 
     end 
    end 
end 

这有错误(预期的响应是a,但是是< 200>)。 但是,当我更改(访问undertaking_path(@undertaking))到(get:show,id:@undertaking)时,它没有错误。访问和获得有什么区别?我读

Rspec and capybara, difference between visit and get methods, with regards to the current_path object

,但我不明白在这种情况下错误。请帮帮我。

无论如何,我的控制器/ undertakings_controller.rb在下面。

 class UndertakingsController < ApplicationController 
     before_action :undertaking_not_have_comment , only: [:show] 
     def show 
      @undertaking=Undertaking.find(params[:id]) 
      @[email protected] 
      @comment=Comment.new do |c| 
      c.user=current_user 
      end 
     end 

     private 
     def undertaking_not_have_comment 
      @undertaking=Undertaking.find(params[:id]) 
      if current_user == @undertaking.user 
       unless @undertaking.comments.count > 0 
       redirect_to root_path 
      end 
     end 
     end 
+0

是 “预期的响应是一个,但<200>” 完整的错误? –

+0

对不起,错误上方有“Failure/Error:expect(response).to redirect_to root_path”。 –

+0

'visit'是在rspec集成/功能规格中使用的水豚方法,'get'通常用于控制器/路由规格。这些都是DSL使测试更具可读性..在后台,访问请求获取路由 – sa77

回答

2

Capybara, being an acceptance test framework, does not expose low-level details like a request or response object. In order to access a web page using Capybara, the developer needs to use the method visit (instead of get). To read the accessed page body, the developer must use page instead of manipulating the response.

你可以阅读更多的 “Improving the integration between Capybara and RSpec

我希望这有助于

+0

链接的文章对其进行了完美的解释,并且Capybara :: DSL方法在控制器规格中不应包含/可用。 –

+0

我会同意他们写得比我好 – MZaragoza

+0

我没有看到它......谢谢! –

相关问题