2012-05-23 17 views
7

मेरे पास सिर्फ एक साधारण सवाल है, लेकिन मुझे कोई जवाब नहीं मिला।रेल - कार्यात्मक परीक्षणों के साथ जेएसओएन एपीआई का परीक्षण

रेल पर मेरी रूबी 3.2.2 अपील के पास एक बुद्धिमान सत्र प्रमाणीकरण के साथ एक JSON API है।

मेरा प्रश्न है: मैं इस एपीआई को कार्यात्मक या एकीकरण परीक्षण के साथ कैसे परीक्षण कर सकता हूं - और क्या सत्र को संभालने का कोई तरीका है?

मेरे पास फ्रंट एंड नहीं है, केवल एक एपीआई है जिसे मैं प्राप्त कर सकता हूं। पद। डाल। और जेएसओएन बॉडी के साथ हटा दें।

इस स्वचालित परीक्षण का सबसे अच्छा तरीका कौन सा है?

उदाहरण नया उपयोगकर्ता

पोस्ट www.exmaple.com/users

{ 
"user":{ 
    "email" : "[email protected]", 
    "password " : "mypass" 
    } 
} 

उत्तर

16

बनाने यह कार्यात्मक परीक्षण के साथ क्या करना आसान है। एक उपयोगकर्ता उदाहरण में मैं Rspec में spec/controllers/users_controller_spec.rb में डाल होगा:

require 'spec_helper' 

describe UsersController do 
    render_views # if you have RABL views 

    before do 
    @user_attributes = { email: "[email protected]", password: "mypass" } 
    end 

    describe "POST to create" do 

    it "should change the number of users" do 
     lambda do 
      post :create, user: @user_attributes 
     end.should change(User, :count).by(1) 
    end 

    it "should be successful" do 
     post :create, user: @user_attributes 
     response.should be_success 
    end 

    it "should set @user" do 
     post :create, user: @user_attributes 
     assigns(:user).email.should == @user_attributes[:email] 
    end 

    it "should return created user in json" do # depend on what you return in action 
     post :create, user: @user_attributes 
     body = JSON.parse(response.body) 
     body["email"].should == @user_attributes[:email] 
     end 
    end 

जाहिर है, आप चश्मा ऊपर अनुकूलन कर सकते हैं, लेकिन यह आपके लिए प्रारंभ करने चाहिए। चीयर्स।

+1

आपने मुझे आरएसपीईसी का उपयोग करने की सिफारिश क्यों की? आरएसपीईसी क्या है और आरएसपीईसी और सामान्य परीक्षणों में अंतर क्या है। – Lailo

+0

"सामान्य परीक्षण" से आपका क्या मतलब है? मुझे बस सुविधाजनक डीएसएल और सहायक तरीकों की सत्यता के लिए रुपेक पसंद है। आप वर्तमान में क्या उपयोग कर रहे हैं? –

1

आप उपयोगकर्ता ककड़ी (BDD) उदाहरण के लिए इस तरह के मामलों का परीक्षण करने, कर सकते हैं: तो

Feature: Successful login 
    In order to login 
    As a user 
    I want to use my super API 

    Scenario: List user 
    Given the system knows about the following user: 
     | email   | username | 
     | [email protected] | blabla | 
    When the user requests POST /users 
    Then the response should be JSON: 
    """ 
    [ 
     {"email": "[email protected]", "username": "blabla"} 
    ] 
    """ 

, तुम बस जहां pickle अपने चरणों में लिखने के लिए, की जरूरत है मणि 'बहुत उपयोगी

संबंधित मुद्दे

 संबंधित मुद्दे