我正在开发一个rails项目(rails 3.2.12和ruby1.9.3),我正在尝试测试我的一个控制器。我有个控制器:
class PostsController < ApplicationController
def index
@posts = Post.all
end
end我的测试是:
require "spec_helper"
describe PostsController do
let(:post){FactoryGirl.create(:post)}
context "JSON" do
describe "GET #index" do
it "should access index" do
get :index
end
end
end
end还有我的routes.rb
AuthApp::Application.routes.draw do
resources :posts
end但跑完之后
$ rspec spec我发现了一个错误:
1) PostsController JSON GET #index should access index
Failure/Error: get :index
ActionView::MissingTemplate:
Missing template posts/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :jbuilder]}. Searched in:
* "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007fbfb62d64a0>"
# ./spec/controllers/post_controller_spec.rb:9:in `block (4 levels) in <top (required)>'如何使用索引操作而不是/post/ index访问/posts?
提前谢谢你,
发布于 2014-01-03 11:30:35
谢谢你的回答。我遇到的问题是,我的模板是jbuilder模板,而规范在默认情况下不会呈现它们。
我在github中使用了这个链接。
发布于 2014-08-31 03:02:25
默认情况下,rspec配置为不呈现视图(请看这里),它可以在spec/support/spec_helper.rb (或spec/support/rails_helper.rb)中进行如下更改:
RSpec.configure do |config|
config.render_views = true
end也可以在需要的地方将块添加到规范文件中:
require 'rails_helper'
RSpec.configure do |config|
config.render_views = true
end
describe SessionsController, :type => :controller do
... 发布于 2013-12-31 11:08:05
将格式指定为json
it "........" do
expected = {...}.to_json
get :index, :format => :json
response.body.should == expected
endhttps://stackoverflow.com/questions/20856110
复制相似问题