但使用 <[]> 渲染-6ren"> 但使用 <[]> 渲染-gem 文件: source 'https://rubygems.org' gem 'rails', '4.1.1' gem 'mysql2' gem 'sass-rails', '~> 4.0.3'-6ren">
gpt4 book ai didi

ruby-on-rails - 期望 <"index"> 但使用 <[]> 渲染

转载 作者:行者123 更新时间:2023-12-02 17:42:13 26 4
gpt4 key购买 nike

gem 文件:

source 'https://rubygems.org'
gem 'rails', '4.1.1'
gem 'mysql2'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'tzinfo-data', platforms: [:mingw, :mswin]
gem 'devise'
gem 'cancan'
gem 'attr_encrypted'
gem 'faker', '1.0.1'
gem 'jdbc-mysql', '5.1.28', :platform => :jruby
gem 'thinking-sphinx', '~> 3.1.0'
gem 'kaminari'
gem 'postmark-rails', '~> 0.5.0'
gem 'slim-rails'
gem 'strong_password', '~> 0.0.3'
gem 'jquery-turbolinks'
gem 'whenever', :require => false
gem 'nokogiri'
gem 'mechanize'
gem 'prawn'
gem 'render_csv', '~> 2.0.0'
gem 'capistrano', '~> 3.2.0'

group :development, :test do
gem "capybara", "2.2.1"
gem "cucumber-rails", "~> 1.3.0", require: false
gem 'rspec-rails', '~> 3.0.0'
gem "factory_girl_rails", "~> 4.1.0"
end

gem 'hirb', group: :development

spec/spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment",__FILE__)
require 'rspec/rails'
require "capybara/rspec"
include Capybara::DSL

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
end

spec/support/controller_macros.rb

module ControllerMacros
def login_admin
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryGirl.create(:admin) # Using factory girl as an example
end
end
end

spec/factories/factory.rb

FactoryGirl.define do
factory :user do
email "test@test.com"
password "12345678"
end
end

categories_controller.rb

class CategoriesController < ApplicationController
before_filter :authenticate_user!, :load_key!

def index
@categories = Category.where("user_id is null or user_id = ?", current_user).order(updated_at: :desc)
end
private

def category_params
params.require(:category).permit(:title)
end
end

spec/controllers/categories_controller_spec.rb

require 'rails_helper'

describe CategoriesController do
login_user

it "redirect when something went wrong" do
get :index
expect(response).to render_template("index")
end
end

给我:

1) 出现问题时,CategoriesController 重定向
失败/错误:expect(response).to render_template("index")
期待 <"index"> 但使用 <[]>
渲染

最佳答案

首先,检查您的操作是否按预期工作,为此,在最后一行添加一些输出并查看它是否已打印。那么,你应该使用 render_views如果你想像这样测试模板渲染

require 'rails_helper'

describe CategoriesController do
render_views
login_user

it "redirect when something went wrong" do
get :index
expect(response).to render_template("index")
end
end

关于ruby-on-rails - 期望 <"index"> 但使用 <[]> 渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24653543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com