[英]Simple controller tests with rspec rails4
我最近正在研究一些rspec測試,我想知道如何正確測試控制器。 我的控制器非常簡單,所以它不應該太難:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
def index
@q = User.search(params[:q])
@users = @q.result(distinct: true)
@q.build_condition if @q.conditions.empty?
@q.build_sort if @q.sorts.empty?
end
# GET /users/1
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
def archive
@q = User.search(params[:q])
@users = @q.result(distinct: true)
@q.build_condition if @q.conditions.empty?
@q.build_sort if @q.sorts.empty?
end
# POST /users
def create
@user = User.new(user_params)
if @user.save
redirect_to users_path, notice: 'Student was successfully added.'
else
render action: 'new'
end
end
# PATCH/PUT /users/1
def update
if @user.update(user_params)
redirect_to @user, notice: 'Student information was successfully updated.'
else
render action: 'edit'
end
end
# DELETE /users/1
def destroy
@user.destroy
redirect_to users_url, notice: 'Student information was successfully deleted.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:firstName, :lastName, :email, :dateOfBirth, :notes, :sex, :archive, :category => [])
end
end
到目前為止,我已經寫了2-3個測試,但我不確定他們是否做了什么:
describe 'GET #index' do
it "displays all users" do
get :index
response.should be_redirect
end
end
describe 'GET #new' do
it "creates a new user" do
get :new
response.should be_redirect
end
end
我嘗試做同樣的編輯和節目,但他們沒有工作,我不知道為什么(因為我說,我不知道我在做什么)。 任何人都可以給我一些這些方法的測試示例,或者可以將我重定向到rails4的rspec指南?
你期望控制器#index動作重定向嗎? 因為那不典型。 我會
describe 'GET #index' do
get 'index'
it {expect(response).to be_success)}
end
這條線......
it "displays all users" do
在控制器規格讓我想知道你的混亂控制器和請求規格。 我第一次參加測試時就這樣做了。 “顯示所有用戶”聽起來像是一個請求規范給我。 測試頁面重定向或響應狀態代碼是否更類似於控制器規范。
我發現http://betterspecs.org/是一個非常有用的資源,可以更好地理解測試。
RE:測試什么
這對我有用,但結果可能會有所不同。
控制器規格 - 不要測試控制器
控制器應該是瘦的,所以你只是測試Rails是否正常工作。 例如,一個索引動作可能包含@users = User.all或類似的東西,其他很少。 那里有什么測試? 沒有。 如果您的控制器操作中有很多代碼,那么它可能不應該存在。 將其移至模型。 記住:胖子模特,瘦小的控制者。 這是測試如何創建更好的代碼的示例。 我的控制器規格很少,我認為幾乎所有這些都是對頁面的雙重檢查授權。 我只在控制器中有代碼的地方使用它們。 這是一個例子:
context "Non admin signed in" do
before(:each) do
sign_in user
controller.stub!(:current_user).and_return(user)
end
it {subject.current_user.should_not be_nil}
it "deny non admin access to index" do
sign_in user
get 'index'
expect(response).to render_template("pages/access_denied")
end
結束
請求規范測試您在瀏覽器中測試的內容(20%的測試)
想象一下,您沒有進行RSpec測試。 如果你像我一樣,那么這並不難想象。 你會如何測試你想要構建的東西? 您可能要做的第一件事就是加載瀏覽器並查看頁面上是否有您期望的內容。 這是一個請求規范。 就這么簡單。 請求規范是加載瀏覽器,點擊幾個按鈕並檢查發生的事情的自動方式。 無論你在瀏覽器中檢查是什么......使用Capybara檢查同樣的事情。 如果它在頁面上有Javascript,那么你需要在Capybara頂部使用Webkit或Selenium來按下按鈕。 使用selenium,您實際上會看到桌面上彈出的瀏覽器窗口,好像一個神秘的gremlin控制了您的鍵盤。 不要在請求規范中測試任何您不會在瀏覽器中手動測試的內容。 這意味着不要檢查數據庫中其他模型的狀態。 請求規范是用戶可以看到的。 如果你看不到它,不要測試它。
型號規格 - 測試你在控制台測試的內容(80%的測試)在我成為一個優秀的TDD / BDD男孩之前,我發現我花了很多時間加載irb或控制台並制作模型並做X以查看Y是否會發生。 自動化那件事。 這是一個模型規范。 當您的請求規范失敗時(如果它正在執行任何有用的操作,它應該首先應該),然后下拉到模型規范中。 失敗的請求規范可能是:
it {expect(page.find('#login_box')).to have_content 'Logged in as Kevin Monk'}
從
no method full_name for instance of User
如果你不是TDD好男孩,你可以加載控制台並找到full_name方法發生的事情。
> rails console
$> kevin = User.find(1)
$> kevin.full_name
然后目視檢查您是否獲得了全名baack,但這應該作為型號規格來完成。
我希望有所幫助。 我在很多關於測試的書籍中遇到的一個問題是,作者傾向於成為這樣的專家,因此不會理解我們凡人真正需要理解你應該測試的基本前提。
你的規范代碼中有拼寫錯誤,你必須更改respone,以獲得響應
我認為這就是問題所在
您可以在中找到有關測試控制器的更多信息
https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.