如何为 returns 对象的方法编写控制器规范,但不检查任何相关模板
How to write controller spec for method which returns an object, but without checking for any related templates
我想通过基本示例以更综合的方式提出我的问题。
1) I have UsersController
2) I have show method in it
3) But I don't have any templates for this method(either - show.html.erb/show.xml.erb etc)
4) So currently, I just wanted to write a spec for just checking whether my method is returning an object or not
5) it is working, but as it is CRUD related method, it is expecting a Template for it.
6) ActionView::MissingTemplate
我试过这种方式
it "should show the user record" do
get :show, :id => 1
User.should_receive(:find).at_least(:once).with(1).and_return(mock_user)
end
class UsersController
def show
# params = {}
# params = {:id => 1}
@user_obj = User.find(params[:id]) # ActiveRecord::Relation
end
end
require 'spec_helper'
describe UsersController do
def mock_user(stubs={})
@mock_user ||= mock_model(User, stubs).as_null_object
end
it "should show the user record" do
expect(assigns(:user_obj)).to eq(mock_user) # How to stop execution here only.
end
end
ActionView::MissingTemplate: Missing template users/show with
{:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html],
:locale=>[:en, :en]} in view paths
"#"
您尝试做的事情似乎不适合我。
这种类型的测试(controller tests)用于测试动作和渲染的响应。所以你不应该那样做,你应该测试动作渲染的输出。否则,如果您只想测试方法 show
(单元测试)
不要在这里使用 get
你可以这样做
users_controller = UsersController.new
users_controller.show
并将其作为任何 class
中的任何方法进行测试
您缺少渲染,因此它会搜索要渲染的模板。
class UsersController
def show
# params = {}
# params = {:id => 1}
@user_obj = User.find(params[:id])
render status: 200, json: @user_obj
end
end
现在你的规范应该可以工作了。
我想通过基本示例以更综合的方式提出我的问题。
1) I have UsersController
2) I have show method in it
3) But I don't have any templates for this method(either - show.html.erb/show.xml.erb etc)
4) So currently, I just wanted to write a spec for just checking whether my method is returning an object or not
5) it is working, but as it is CRUD related method, it is expecting a Template for it.
6) ActionView::MissingTemplate
我试过这种方式
it "should show the user record" do
get :show, :id => 1
User.should_receive(:find).at_least(:once).with(1).and_return(mock_user)
end
class UsersController
def show
# params = {}
# params = {:id => 1}
@user_obj = User.find(params[:id]) # ActiveRecord::Relation
end
end
require 'spec_helper'
describe UsersController do
def mock_user(stubs={})
@mock_user ||= mock_model(User, stubs).as_null_object
end
it "should show the user record" do
expect(assigns(:user_obj)).to eq(mock_user) # How to stop execution here only.
end
end
ActionView::MissingTemplate: Missing template users/show with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "#"
您尝试做的事情似乎不适合我。
这种类型的测试(controller tests)用于测试动作和渲染的响应。所以你不应该那样做,你应该测试动作渲染的输出。否则,如果您只想测试方法 show
(单元测试)
不要在这里使用 get
你可以这样做
users_controller = UsersController.new
users_controller.show
并将其作为任何 class
中的任何方法进行测试您缺少渲染,因此它会搜索要渲染的模板。
class UsersController
def show
# params = {}
# params = {:id => 1}
@user_obj = User.find(params[:id])
render status: 200, json: @user_obj
end
end
现在你的规范应该可以工作了。