`

rails Rspec测试框架

阅读更多

Rspec rails的测试框架

 

gem 'rspec-rails' ||  https://github.com/rspec/rspec-rails

 

# 添加gem到Gemfile

group :development, :test do

  gem 'rspec-rails', '~> 2.0'

end 

 

# 安装Rspec

rails generate rspec:install

 

# rspec 执行命令, bundle exec 是用你项目下的版本执行

bundle exec rspec

 

# 测试model下的文件有没有通过

bundle exec rspec spec/models

 

# 单写一个文件时测试这个文件通过没有

bundle exec rspec spec/controllers/accounts_controller_spec.rb

 

require "spec_helper"

# 顶级的描述
describe PostsController do
   # 下一级的描述,例如一个方法的描述
  describe "GET #index" do
    # 场景的描述
    context "get all posts"
     # 实际问题的描述
    it "responds successfully with an HTTP 200 status code" do
       ......
    end
  end
end

# 一般用这些来描述你的测试用例,输出的时候也有缩进,这样能更好的查找错误。

 

require 'spec_helper'

describe CommentsController do
  describe "POST create" do
    # 模拟登陆,current_user 就可以用了
    login_user

    context "***" do
      it "***" do
        # post这个方法第一个参数就是action,第二个参数就是params里的参数 
        post :create, {id: album.id, body: 'album comment', type: 'Album'}
        #  expect 是所期望的,执行的结果里有got的话,意思实际结果
        expect()
      end
    end
end

#  expect(assigns(:comment))  期望controller里的@comment这个变量的值为... 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics