2016-11-10 6 views
1

posts_controller.rb失敗/エラー:response.should render_template(新しい)<"new">を期待したが<"">

class PostsController < ApplicationController 
    respond_to :html, :xml, :json 
    before_filter :authenticate_user!, :except => [:show, :index] 
    before_filter :admin_only, :except => [:show, :index] 

    def new 
    @post = Post.new 
    end 
end 

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'forgery' 
require 'populators' 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

RSpec.configure do |config| 
    config.mock_with :rspec 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/test/fixtures" 

    config.before(:suite) do 
    DatabaseCleaner.strategy = :truncation 
    end 

    config.after(:suite) do 
    DatabaseCleaner.clean 
    end 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = false 
end 

posts_controller_spec.rbでレンダリング

require File.dirname(__FILE__) + '/../spec_helper' 

describe PostsController do 
    fixtures :all 
    include Devise::TestHelpers 
    render_views 

    before(:each) do 
    DatabaseCleaner.strategy = :truncation 
    DatabaseCleaner.start 
    end 

    after(:each) do 
    DatabaseCleaner.clean 
    end 
    it "new action should render new template" do 
    get :new 
    response.should render_template(:new) 
    end 
end 

ruby 1.8.7, rspec 2.11, Rails 3.2.19

私はが<「新しい」期待し、このエラーを取得します>が、私はこのような場合は...私は多くの提案を試みたが、いずれかの成功を得るdidntは渡すことができますどのように..助けてくださいAmが立ち往生>「」<でレンダリングそれに... :(

注:私はこれは私がhttps://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specsを使用すると、@Зелёныありがとう働くコントローラのコード

+0

のためにあなたは、アクション 'new'、' before_filterをユーザーに許可していない[:ショーを、:インデックス] '。 –

+0

@Зелёныйには何か回避策がありますか? –

+0

明らかに、ユーザーの承認を得ています。 –

答えて

0
# Use the sign_in helper to sign in a fixture `User` record. 
user = users(:one) 

@request.env['warden'].stub(:authenticate!).and_return(user) 
@controller.stub(:current_user).and_return(user) 

get :new 
assert_template 'new' 

を変更傾けます> =除く:authenticate_user!:й方向:)

関連する問題