加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 匿名控制器的Rspec stubing视图

发布时间:2020-12-16 20:31:05 所属栏目:百科 来源:网络整理
导读:我试图在应用程序控制器上测试一个将用作之前的过滤器的方法.为了做到这一点,我在我的测试中设置了一个匿名控制器,应用了以前的过滤器,以确保它正常工作. 测试目前看起来像这样: describe ApplicationController do controller do before_filter :authentic
我试图在应用程序控制器上测试一个将用作之前的过滤器的方法.为了做到这一点,我在我的测试中设置了一个匿名控制器,应用了以前的过滤器,以确保它正常工作.

测试目前看起来像这样:

describe ApplicationController do
  controller do
    before_filter :authenticated

    def index      
    end
  end

  describe "user authenticated" do
    let(:session_id){"session_id"}
    let(:user){OpenStruct.new(:email => "pythonandchips@gmail.com",:name => "Colin Gemmell")}

    before do
      request.cookies[:session_id] = session_id
      UserSession.stub!(:find).with(session_id).and_return(user)
      get :index
    end

    it { should assign_to(:user){user} }

  end
end

应用程序控制器是这样的:

class ApplicationController < ActionController::Base
  protect_from_forgery

  def authenticated
    @user = nil
  end
end

我的问题是,当我运行测试我得到以下错误

1) ApplicationController user authenticated 
   Failure/Error: get :index
   ActionView::MissingTemplate:
     Missing template stub_resources/index with {:handlers=>[:erb,:rjs,:builder,:rhtml,:rxml,:haml],:formats=>[:html],:locale=>[:en,:en]} in view paths "#<RSpec::Rails::ViewRendering::PathSetDelegatorResolver:0x984f310>"

根据文档,视图不是rendered when running controller tests,但是这表示这个动作没有存根(这是可以理解的,因为视图不存在)

任何人都有一个线索,如何解决这个问题或存根的看法.

干杯
Colin G

解决方法

你不可以这样做:
render :nothing => true

里面的#index动作?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读