3

我正在使用RSpec ( Rails 4.2.5上的 3.4 )测试视图。我在我的 CRUD 控制器中使用了体面的曝光 gem 体面的曝光使定义命名方法变得容易,这些命名方法可用于我的视图并记住结果值。

但我不明白如何在视图规范中存根这些方法。我尝试根据 RSpec 文档执行此操作,但会引发错误。

为什么它不起作用?如何article在视图中存根方法?

我的控制器

class Account::ArticlesController < Account::BaseController
  expose(:articles) { current_user.articles }
  expose(:article, attributes: :article_params)

  # POST /account/articles
  # POST /account/articles.json
  def create
    respond_to do |format|
      if article.save
        format.html { redirect_to account_article_url(article), notice: 'Article was successfully created.' }
        format.json { render :show, status: :created, location: account_article_url(article) }
      else
        format.html { render :new }
        format.json { render json: article.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /account/articles/1
  # PATCH/PUT /account/articles/1.json
  def update
    respond_to do |format|
      if article.update(article_params)
        format.html { redirect_to account_article_url(article), notice: 'Article was successfully updated.' }
        format.json { render :show, status: :ok, location: account_article_url(article) }
      else
        format.html { render :edit }
        format.json { render json: article.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /account/articles/1
  # DELETE /account/articles/1.json
  def destroy
    respond_to do |format|
      if article.destroy
        format.html { redirect_to account_articles_url, notice: 'Article was successfully destroyed.' }
        format.json { head :no_content }
      else
        format.html { redirect_to account_articles_url, notice: 'Article was not destroyed.' }
        format.json { render json: article.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def article_params
    params.require(:article).permit(:title, :announce, :content, :is_published, :published_at)
  end

end

我有一个视图规范

require 'rails_helper'

RSpec.describe 'account/articles/new', type: :view do

  before do
    allow(view).to receive(:article).and_return(build(:article))
  end

  it 'renders new article form' do
    render
    expect(rendered).to match %Q{form[action=#{account_articles_path}][method=post]}
  end
end

但这会引发错误

Failures:

  1) account/articles/new renders new article form
     Failure/Error: allow(view).to receive(:article).and_return(build(:article))
       #<#<Class:0x007fe85c2c7620>:0x007fe85d161208 @_routes=nil, @_config={}, @view_renderer=#<ActionView::Renderer:0x007fe85d1613c0 @lookup_context=#<ActionView::LookupContext:0x007fe85d1615a0 @details_key=nil, @details={:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}, @skip_default_locale=false, @cache=true, @prefixes=["", "account/articles"], @rendered_format=nil, @view_paths=#<ActionView::PathSet:0x007fe85d1614d8 @paths=[#<ActionView::OptimizedFileSystemResolver:0x007fe85d37fb48 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fe85d37fb20 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fe85d37faf8 @backend={}, @default_proc=#<Proc:0x007fe85f071198@/Users/et/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5/lib/action_view/template/resolver.rb:49 (lambda)>>>, @path="/Users/et/Documents/projects/test-blog/app/views">, #<ActionView::OptimizedFileSystemResolver:0x007fe85d37feb8 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fe85d37fe90 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fe85d37fe68 @backend={}, @default_proc=#<Proc:0x007fe85f071198@/Users/et/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5/lib/action_view/template/resolver.rb:49 (lambda)>>>, @path="/Users/et/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/app/views">]>>>, @request=#<ActionController::TestRequest:0x007fe85c35e048 @env={"rack.version"=>[1, 3], "rack.input"=>#<StringIO:0x007fe85f87eca0>, "rack.errors"=>#<StringIO:0x007fe85f87ed40>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0", "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0", "HTTP_USER_AGENT"=>"Rails Testing", "action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0x007fe85c6713e8>, "action_dispatch.parameter_filter"=>[:password], "action_dispatch.redirect_filter"=>[], "action_dispatch.secret_token"=>nil, "action_dispatch.secret_key_base"=>"eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", "action_dispatch.show_exceptions"=>false, "action_dispatch.show_detailed_exceptions"=>true, "action_dispatch.logger"=>#<ActiveSupport::Logger:0x007fe85d32d528 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007fe85d32d4b0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007fe85d2fc928 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007fe85d32d460 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/Users/et/Documents/projects/test-blog/log/test.log>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007fe85d32d438 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fe85d32d3e8>>>>, "action_dispatch.backtrace_cleaner"=>#<Rails::BacktraceCleaner:0x007fe85c2f4aa8 @filters=[#<Proc:0x007fe85c2f4878@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:14>, #<Proc:0x007fe85c2f4850@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:15>, #<Proc:0x007fe85c2f47d8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:16>, #<Proc:0x007fe85c2f5750@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:29>], @silencers=[#<Proc:0x007fe85c2f59a8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:19>], @root="/Users/et/Documents/projects/test-blog/">, "action_dispatch.key_generator"=>#<ActiveSupport::CachingKeyGenerator:0x007fe85f4cc580 @key_generator=#<ActiveSupport::KeyGenerator:0x007fe85f4cc5a8 @secret="eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", @iterations=1000>, @cache_keys=#<ThreadSafe::Cache:0x007fe85f4cc558 @backend={"signed_global_ids64"=>"0\xF4/#*5\xF2\x0F\xB2\x101\v~\xC5\x89\xFF\xDB\x11\xCB(U\xFA\xF87\x13Evz:\xAD\xD6\xA3r\x96\x9A\x82\x17\x84\tR\xF2A\xADV\x85u?^N\xBCT\x84\x10\x11\xD9\xC9>n\xA1\f\x06\xAAuU"}, @default_proc=nil>>, "action_dispatch.http_auth_salt"=>"http authentication", "action_dispatch.signed_cookie_salt"=>"signed cookie", "action_dispatch.encrypted_cookie_salt"=>"encrypted cookie", "action_dispatch.encrypted_signed_cookie_salt"=>"signed encrypted cookie", "action_dispatch.cookies_serializer"=>:json, "action_dispatch.cookies_digest"=>nil, "rack.session"=>{}, "rack.session.options"=>{:key=>"rack.session", :path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :sidbits=>128, :cookie_only=>true, :secure_random=>SecureRandom, :id=>"8a8b5f8906289bb00109022394af0832"}, "action_dispatch.request.path_parameters"=>{:controller=>"account/articles", :action=>"new"}}, @filtered_parameters=nil, @filtered_env=nil, @filtered_path=nil, @protocol=nil, @port=nil, @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil, @uuid=nil>, @response=#<ActionController::TestResponse:0x007fe85c2cf0a0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fe85c2cef38>, @stream=#<ActionDispatch::Response::Buffer:0x007fe85c2cedd0 @response=#<ActionController::TestResponse:0x007fe85c2cf0a0 ...>, @buf=[], @closed=false>, @header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff"}, @status=200, @sending_file=false, @blank=false, @cv=#<MonitorMixin::ConditionVariable:0x007fe85c2ced58 @monitor=#<ActionController::TestResponse:0x007fe85c2cf0a0 ...>, @cond=#<Thread::ConditionVariable:0x007fe85c2ced30>>, @committed=false, @sending=false, @sent=false, @content_type=nil, @charset=nil, @cache_control={}, @etag=nil>, @params={}, @_assigns={"request"=>#<ActionController::TestRequest:0x007fe85c35e048 @env={"rack.version"=>[1, 3], "rack.input"=>#<StringIO:0x007fe85f87eca0>, "rack.errors"=>#<StringIO:0x007fe85f87ed40>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0", "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0", "HTTP_USER_AGENT"=>"Rails Testing", "action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0x007fe85c6713e8>, "action_dispatch.parameter_filter"=>[:password], "action_dispatch.redirect_filter"=>[], "action_dispatch.secret_token"=>nil, "action_dispatch.secret_key_base"=>"eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", "action_dispatch.show_exceptions"=>false, "action_dispatch.show_detailed_exceptions"=>true, "action_dispatch.logger"=>#<ActiveSupport::Logger:0x007fe85d32d528 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007fe85d32d4b0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007fe85d2fc928 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007fe85d32d460 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/Users/et/Documents/projects/test-blog/log/test.log>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007fe85d32d438 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fe85d32d3e8>>>>, "action_dispatch.backtrace_cleaner"=>#<Rails::BacktraceCleaner:0x007fe85c2f4aa8 @filters=[#<Proc:0x007fe85c2f4878@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:14>, #<Proc:0x007fe85c2f4850@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:15>, #<Proc:0x007fe85c2f47d8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:16>, #<Proc:0x007fe85c2f5750@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:29>], @silencers=[#<Proc:0x007fe85c2f59a8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:19>], @root="/Users/et/Documents/projects/test-blog/">, "action_dispatch.key_generator"=>#<ActiveSupport::CachingKeyGenerator:0x007fe85f4cc580 @key_generator=#<ActiveSupport::KeyGenerator:0x007fe85f4cc5a8 @secret="eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", @iterations=1000>, @cache_keys=#<ThreadSafe::Cache:0x007fe85f4cc558 @backend={"signed_global_ids64"=>"0\xF4/#*5\xF2\x0F\xB2\x101\v~\xC5\x89\xFF\xDB\x11\xCB(U\xFA\xF87\x13Evz:\xAD\xD6\xA3r\x96\x9A\x82\x17\x84\tR\xF2A\xADV\x85u?^N\xBCT\x84\x10\x11\xD9\xC9>n\xA1\f\x06\xAAuU"}, @default_proc=nil>>, "action_dispatch.http_auth_salt"=>"http authentication", "action_dispatch.signed_cookie_salt"=>"signed cookie", "action_dispatch.encrypted_cookie_salt"=>"encrypted cookie", "action_dispatch.encrypted_signed_cookie_salt"=>"signed encrypted cookie", "action_dispatch.cookies_serializer"=>:json, "action_dispatch.cookies_digest"=>nil, "rack.session"=>{}, "rack.session.options"=>{:key=>"rack.session", :path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :sidbits=>128, :cookie_only=>true, :secure_random=>SecureRandom, :id=>"8a8b5f8906289bb00109022394af0832"}, "action_dispatch.request.path_parameters"=>{:controller=>"account/articles", :action=>"new"}}, @filtered_parameters=nil, @filtered_env=nil, @filtered_path=nil, @protocol=nil, @port=nil, @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil, @uuid=nil>, "response"=>#<ActionController::TestResponse:0x007fe85c2cf0a0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fe85c2cef38>, @stream=#<ActionDispatch::Response::Buffer:0x007fe85c2cedd0 @response=#<ActionController::TestResponse:0x007fe85c2cf0a0 ...>, @buf=[], @closed=false>, @header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff"}, @status=200, @sending_file=false, @blank=false, @cv=#<MonitorMixin::ConditionVariable:0x007fe85c2ced58 @monitor=#<ActionController::TestResponse:0x007fe85c2cf0a0 ...>, @cond=#<Thread::ConditionVariable:0x007fe85c2ced30>>, @committed=false, @sending=false, @sent=false, @content_type=nil, @charset=nil, @cache_control={}, @etag=nil>, "params"=>{}}, @_controller=#<ActionView::TestCase::TestController:0x007fe85c35e200 @_action_has_layout=true, @_routes=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil, @request=#<ActionController::TestRequest:0x007fe85c35e048 @env={"rack.version"=>[1, 3], "rack.input"=>#<StringIO:0x007fe85f87eca0>, "rack.errors"=>#<StringIO:0x007fe85f87ed40>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0", "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0", "HTTP_USER_AGENT"=>"Rails Testing", "action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0x007fe85c6713e8>, "action_dispatch.parameter_filter"=>[:password], "action_dispatch.redirect_filter"=>[], "action_dispatch.secret_token"=>nil, "action_dispatch.secret_key_base"=>"eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", "action_dispatch.show_exceptions"=>false, "action_dispatch.show_detailed_exceptions"=>true, "action_dispatch.logger"=>#<ActiveSupport::Logger:0x007fe85d32d528 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007fe85d32d4b0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007fe85d2fc928 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007fe85d32d460 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/Users/et/Documents/projects/test-blog/log/test.log>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007fe85d32d438 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fe85d32d3e8>>>>, "action_dispatch.backtrace_cleaner"=>#<Rails::BacktraceCleaner:0x007fe85c2f4aa8 @filters=[#<Proc:0x007fe85c2f4878@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:14>, #<Proc:0x007fe85c2f4850@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:15>, #<Proc:0x007fe85c2f47d8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:16>, #<Proc:0x007fe85c2f5750@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:29>], @silencers=[#<Proc:0x007fe85c2f59a8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:19>], @root="/Users/et/Documents/projects/test-blog/">, "action_dispatch.key_generator"=>#<ActiveSupport::CachingKeyGenerator:0x007fe85f4cc580 @key_generator=#<ActiveSupport::KeyGenerator:0x007fe85f4cc5a8 @secret="eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", @iterations=1000>, @cache_keys=#<ThreadSafe::Cache:0x007fe85f4cc558 @backend={"signed_global_ids64"=>"0\xF4/#*5\xF2\x0F\xB2\x101\v~\xC5\x89\xFF\xDB\x11\xCB(U\xFA\xF87\x13Evz:\xAD\xD6\xA3r\x96\x9A\x82\x17\x84\tR\xF2A\xADV\x85u?^N\xBCT\x84\x10\x11\xD9\xC9>n\xA1\f\x06\xAAuU"}, @default_proc=nil>>, "action_dispatch.http_auth_salt"=>"http authentication", "action_dispatch.signed_cookie_salt"=>"signed cookie", "action_dispatch.encrypted_cookie_salt"=>"encrypted cookie", "action_dispatch.encrypted_signed_cookie_salt"=>"signed encrypted cookie", "action_dispatch.cookies_serializer"=>:json, "action_dispatch.cookies_digest"=>nil, "rack.session"=>{}, "rack.session.options"=>{:key=>"rack.session", :path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :sidbits=>128, :cookie_only=>true, :secure_random=>SecureRandom, :id=>"8a8b5f8906289bb00109022394af0832"}, "action_dispatch.request.path_parameters"=>{:controller=>"account/articles", :action=>"new"}}, @filtered_parameters=nil, @filtered_env=nil, @filtered_path=nil, @protocol=nil, @port=nil, @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil, @uuid=nil>, @response=#<ActionController::TestResponse:0x007fe85c2cf0a0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fe85c2cef38>, @stream=#<ActionDispatch::Response::Buffer:0x007fe85c2cedd0 @response=#<ActionController::TestResponse:0x007fe85c2cf0a0 ...>, @buf=[], @closed=false>, @header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff"}, @status=200, @sending_file=false, @blank=false, @cv=#<MonitorMixin::ConditionVariable:0x007fe85c2ced58 @monitor=#<ActionController::TestResponse:0x007fe85c2cf0a0 ...>, @cond=#<Thread::ConditionVariable:0x007fe85c2ced30>>, @committed=false, @sending=false, @sent=false, @content_type=nil, @charset=nil, @cache_control={}, @etag=nil>, @params={}, @_view_context_class=#<Class:0x007fe85c2c7620>, @_lookup_context=#<ActionView::LookupContext:0x007fe85d1615a0 @details_key=nil, @details={:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}, @skip_default_locale=false, @cache=true, @prefixes=["", "account/articles"], @rendered_format=nil, @view_paths=#<ActionView::PathSet:0x007fe85d1614d8 @paths=[#<ActionView::OptimizedFileSystemResolver:0x007fe85d37fb48 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fe85d37fb20 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fe85d37faf8 @backend={}, @default_proc=#<Proc:0x007fe85f071198@/Users/et/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5/lib/action_view/template/resolver.rb:49 (lambda)>>>, @path="/Users/et/Documents/projects/test-blog/app/views">, #<ActionView::OptimizedFileSystemResolver:0x007fe85d37feb8 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fe85d37fe90 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fe85d37fe68 @backend={}, @default_proc=#<Proc:0x007fe85f071198@/Users/et/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5/lib/action_view/template/resolver.rb:49 (lambda)>>>, @path="/Users/et/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/app/views">]>>, @_view_renderer=#<ActionView::Renderer:0x007fe85d1613c0 @lookup_context=#<ActionView::LookupContext:0x007fe85d1615a0 @details_key=nil, @details={:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}, @skip_default_locale=false, @cache=true, @prefixes=["", "account/articles"], @rendered_format=nil, @view_paths=#<ActionView::PathSet:0x007fe85d1614d8 @paths=[#<ActionView::OptimizedFileSystemResolver:0x007fe85d37fb48 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fe85d37fb20 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fe85d37faf8 @backend={}, @default_proc=#<Proc:0x007fe85f071198@/Users/et/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5/lib/action_view/template/resolver.rb:49 (lambda)>>>, @path="/Users/et/Documents/projects/test-blog/app/views">, #<ActionView::OptimizedFileSystemResolver:0x007fe85d37feb8 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fe85d37fe90 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fe85d37fe68 @backend={}, @default_proc=#<Proc:0x007fe85f071198@/Users/et/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5/lib/action_view/template/resolver.rb:49 (lambda)>>>, @path="/Users/et/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/app/views">]>>>, @_config={}>, @_request=#<ActionController::TestRequest:0x007fe85c35e048 @env={"rack.version"=>[1, 3], "rack.input"=>#<StringIO:0x007fe85f87eca0>, "rack.errors"=>#<StringIO:0x007fe85f87ed40>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0", "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0", "HTTP_USER_AGENT"=>"Rails Testing", "action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0x007fe85c6713e8>, "action_dispatch.parameter_filter"=>[:password], "action_dispatch.redirect_filter"=>[], "action_dispatch.secret_token"=>nil, "action_dispatch.secret_key_base"=>"eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", "action_dispatch.show_exceptions"=>false, "action_dispatch.show_detailed_exceptions"=>true, "action_dispatch.logger"=>#<ActiveSupport::Logger:0x007fe85d32d528 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007fe85d32d4b0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007fe85d2fc928 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007fe85d32d460 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/Users/et/Documents/projects/test-blog/log/test.log>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007fe85d32d438 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fe85d32d3e8>>>>, "action_dispatch.backtrace_cleaner"=>#<Rails::BacktraceCleaner:0x007fe85c2f4aa8 @filters=[#<Proc:0x007fe85c2f4878@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:14>, #<Proc:0x007fe85c2f4850@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:15>, #<Proc:0x007fe85c2f47d8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:16>, #<Proc:0x007fe85c2f5750@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:29>], @silencers=[#<Proc:0x007fe85c2f59a8@/Users/et/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/backtrace_cleaner.rb:19>], @root="/Users/et/Documents/projects/test-blog/">, "action_dispatch.key_generator"=>#<ActiveSupport::CachingKeyGenerator:0x007fe85f4cc580 @key_generator=#<ActiveSupport::KeyGenerator:0x007fe85f4cc5a8 @secret="eb526d7f4b0c06b367225a0d32ba8b3c3287ea6d6de798961e6fe33b975d44c2cd3ae9f314f0b96c73029f052d4670bc5a4db850ffc6a73586043089f57bdb3b", @iterations=1000>, @cache_keys=#<ThreadSafe::Cache:0x007fe85f4cc558 @backend={"signed_global_ids64"=>"0\xF4/#*5\xF2\x0F\xB2\x101\v~\xC5\x89\xFF\xDB\x11\xCB(U\xFA\xF87\x13Evz:\xAD\xD6\xA3r\x96\x9A\x82\x17\x84\tR\xF2A\xADV\x85u?^N\xBCT\x84\x10\x11\xD9\xC9>n\xA1\f\x06\xAAuU"}, @default_proc=nil>>, "action_dispatch.http_auth_salt"=>"http authentication", "action_dispatch.signed_cookie_salt"=>"signed cookie", "action_dispatch.encrypted_cookie_salt"=>"encrypted cookie", "action_dispatch.encrypted_signed_cookie_salt"=>"signed encrypted cookie", "action_dispatch.cookies_serializer"=>:json, "action_dispatch.cookies_digest"=>nil, "rack.session"=>{}, "rack.session.options"=>{:key=>"rack.session", :path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :sidbits=>128, :cookie_only=>true, :secure_random=>SecureRandom, :id=>"8a8b5f8906289bb00109022394af0832"}, "action_dispatch.request.path_parameters"=>{:controller=>"account/articles", :action=>"new"}}, @filtered_parameters=nil, @filtered_env=nil, @filtered_path=nil, @protocol=nil, @port=nil, @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil, @uuid=nil>, @view_flow=#<ActionView::OutputFlow:0x007fe85a962d88 @content={}>, @output_buffer="", @virtual_path=nil, @rendered_views=#<ActionView::TestCase::Behavior::RenderedViewsCollection:0x007fe85a962b30 @rendered_views={}>> does not implement: article
     # ./spec/views/account/articles/new.html.haml_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 1.11 seconds (files took 6.26 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/views/account/articles/new.html.haml_spec.rb:9 # account/articles/new renders new article form
4

2 回答 2

6
require 'rails_helper'

RSpec.describe 'account/articles/new', type: :view do

  it 'renders new article form' do
    stub_article { Article.new }
    render
    expect(rendered).to match(%r{form.+id="new_article".+action=\"#{account_articles_path}".+accept-charset=\"UTF-8\".+method=\"post\"})
  end

  def stub_article(&block)
    controller.singleton_class.class_exec(block) do
      helper_method :article
      define_method :article do
        block.call
      end
    end
  end

end
于 2016-02-09T11:17:50.303 回答
2

你不能存根article因为

  • rspec-mocks 的verify_partial_doubles配置参数设置为true,因此 RSpec 只允许您存根和模拟对象实现的方法,并且
  • 你的控制器实际上并没有实现article,所以 RSpec 不会让你存根它。

你有两个选择:

  1. 禁用verify_partial_doubles. 这是一个坏主意,因为它会让你在 stubbing 和 mocking 中犯错误。尽管如此,如果您必须这样做,请按照以下方法操作:您的 spec_helper.rb 中可能包含以下内容:

    RSpec.configure do |config|
      config.mock_with :rspec do |mocks|
        mocks.verify_partial_doubles = true
      end
    end
    

    将其更改为verify_partial_doubles = false.

  2. 将以下手动“存根”添加到您的示例中:

    def view.article
      build(:article)
    end
    

    article或者,如果视图多次调用,则在方法之外构建文章或仅从方法中返回它。

在rspec-rails Github issue中有关于这种情况的讨论。

于 2016-02-08T01:31:39.717 回答