相关疑难解决方法(0)

如何在请求规范中存根ApplicationController方法

我需要current_user在Rspec/capybara请求规范中存根方法的响应.该方法在ApplicationControllerhelper_method中定义并使用helper_method.该方法应该只返回一个用户ID.在测试中,我希望这种方法每次都返回相同的用户ID.

或者,我可以通过设置session[:user_id]规范(这是什么current_user返回)来解决我的问题...但这似乎也不起作用.

这些都可能吗?

编辑:

这是我得到的(它不工作.它只运行正常的current_user方法).

require 'spec_helper'

describe "Login" do

   before(:each) do
     ApplicationController.stub(:current_user).and_return(User.first)
   end

  it "logs in" do
    visit '/'
    page.should have_content("Hey there user!")
  end

end
Run Code Online (Sandbox Code Playgroud)

也不工作:

require 'spec_helper'

describe "Login" do

  before(:each) do
    @mock_controller = mock("ApplicationController") 
    @mock_controller.stub(:current_user).and_return(User.first)
  end

  it "logs in" do
    visit '/'
    page.should have_content("Hey there user!")
  end

end
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails capybara

63
推荐指数
2
解决办法
3万
查看次数

控制器规范中的RSpec存根辅助方法

发现类似的问题,但令人惊讶的是没有,我发现,给出一个简单的答案......

试图在我的控制器规范中存根一个辅助方法; 不太确定哪个对象需要加倍?

Controller调用此方法:

#app/helpers/sessions_helper.rb

def signed_in?
  current_user.present?
end
Run Code Online (Sandbox Code Playgroud)

我想在规范中将其存根以返回true/false.

rspec ruby-on-rails rspec-rails

10
推荐指数
1
解决办法
9284
查看次数

标签 统计

rspec ×2

ruby-on-rails ×2

capybara ×1

rspec-rails ×1

ruby ×1