我对rails中的功能测试有疑问。
对于前面部分,操作仅索引并显示
#app/controller/themes_controller_rb
class ThemesController < ApplicationController
def index
@themes = Theme.active
end
def show
@theme = Theme.find(params[:id])
end
def new
end
end
和测试
#test/integration/theme_controller_test.rb
require 'test_helper'
class ThemesControllerTest < ActionController::TestCase
def setup
@theme = create(:theme)
end
test "should assign variable on index" do
get :index
assert_response :success
assert_not_nil assigns(:themes)
end
test "should show a theme" do
get :show, {'id' => @theme}
assert_response :success
assert_not_nil assigns(:theme)
end
end
目前没问题
对于admin部分,所有CRUD操作都存在,所以 index和show
#app/controllers/admin/themes_controller.rb
class Admin::ThemesController < Admin::AdminController
layout 'admin/admin'
before_action :admin_user
def index
@themes = Theme.all
end
def show
@theme = Theme.find(params[:id])
end
end
并且测试是相同的
#test/controllers/admin/theme_controller_test.rb
require 'test_helper'
class Admin::ThemesControllerTest < ActionController::TestCase
def setup
@theme = create(:theme)
end
test "should assign variable on index" do
get :index
assert_response :success
assert_not_nil assigns(:themes)
end
test "should show a theme" do
get :show, {'id' => @theme}
assert_response :success
assert_not_nil assigns(:theme)
end
end
但是对于那些最新的测试,我有302响应而不是成功
FAIL["test_should_assign_variable_on_index", Admin::ThemesControllerTest, 2016-03-16 06:50:16 +0000]
test_should_assign_variable_on_index#Admin::ThemesControllerTest (1458111016.61s)
Expected response to be a <success>, but was <302>
test/controllers/admin/themes_controller_test.rb:11:in `block in <class:ThemesControllerTest>'
FAIL["test_should_show_a_theme", Admin::ThemesControllerTest, 2016-03-16 06:50:16 +0000]
test_should_show_a_theme#Admin::ThemesControllerTest (1458111016.62s)
Expected response to be a <success>, but was <302>
test/controllers/admin/themes_controller_test.rb:18:in `block in <class:ThemesControllerTest>'
我做错了什么?感谢您的帮助:)