小编sha*_*han的帖子

用rspec测试ruby中的"create"方法

我在Ruby on Rails中编写了这个控制器代码

class PostsController < ApplicationController
  before_filter :authenticate_user!

  def index  
    @posts = Post.all(:order => "created_at DESC")  
    respond_to do |format|  
      format.html  
    end  
  end  

  def create  
    @post = Post.create(:message => params[:message])  
    respond_to do |format|  
      if @post.save  
        format.html { redirect_to posts_path }  
        format.js
      else  
        flash[:notice] = "Message failed to save."  
        format.html { redirect_to posts_path }  
      end  
    end  
  end  
end
Run Code Online (Sandbox Code Playgroud)

并对应于此我写了以下测试用例: -

require 'spec_helper'

describe PostsController do
  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      response.should be_success
    end
  end

  describe …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

1
推荐指数
1
解决办法
6045
查看次数

标签 统计

rspec ×1

ruby-on-rails ×1