首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rspec控制器基本测试

Rspec控制器基本测试
EN

Stack Overflow用户
提问于 2013-12-31 10:49:19
回答 3查看 3.7K关注 0票数 1

我正在开发一个rails项目(rails 3.2.12和ruby1.9.3),我正在尝试测试我的一个控制器。我有个控制器:

代码语言:javascript
复制
class PostsController < ApplicationController
  def index
    @posts = Post.all
  end
end

我的测试是:

代码语言:javascript
复制
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

代码语言:javascript
复制
 AuthApp::Application.routes.draw do
   resources :posts
 end

但跑完之后

代码语言:javascript
复制
$ rspec spec

我发现了一个错误:

代码语言:javascript
复制
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?

提前谢谢你,

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-03 11:30:35

谢谢你的回答。我遇到的问题是,我的模板是jbuilder模板,而规范在默认情况下不会呈现它们。

我在github中使用了这个链接

票数 1
EN

Stack Overflow用户

发布于 2014-08-31 03:02:25

默认情况下,rspec配置为不呈现视图(请看这里),它可以在spec/support/spec_helper.rb (或spec/support/rails_helper.rb)中进行如下更改:

代码语言:javascript
复制
RSpec.configure do |config| 
  config.render_views = true 
end

也可以在需要的地方将块添加到规范文件中:

代码语言:javascript
复制
require 'rails_helper' 

RSpec.configure do |config| 
  config.render_views = true 
end 

describe SessionsController, :type => :controller do 
... 
票数 4
EN

Stack Overflow用户

发布于 2013-12-31 11:08:05

将格式指定为json

代码语言:javascript
复制
it "........" do
 expected = {...}.to_json
 get :index, :format => :json
 response.body.should == expected
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20856110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档