我在 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
与此相对应,我编写了以下测试用例:-
require 'spec_helper'
describe PostsController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
describe "#create" do
it "creates a successful mesaage post" do
@post = Post.create(message: "Message")
@post.should be_an_instance_of Post
end
end
end
我在这两个方面都失败了。请看一下代码并帮助我弄清楚。