7

簡単な質問がありますが、答えが見つかりませんでした。

私のRuby on Rails 3.2.2アプリケーションには、デバイスセッション認証を備えたJSON APIがあります。

私の質問は、この API を機能テストまたは統合テストでテストするにはどうすればよいですか? また、セッションを処理する方法はありますか?

フロントエンドはなく、GET できる API だけです。役職。置く。そしてJSONボディでDELETE。

この自動化をテストする最良の方法はどれですか?

例 新しいユーザーの作成

POST www.exmaple.com/users

{
 "user":{
    "email" : "test@example.com",
    "password " : "mypass"
  }
}
4

3 に答える 3

16

機能テストを行うのは簡単です。ユーザーの例では、それらをspec/controllers/users_controller_spec.rbRspecに入れます。

 require 'spec_helper'

 describe UsersController do
   render_views # if you have RABL views

   before do
     @user_attributes = { email: "test@example.com", 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

明らかに、上記の仕様を最適化できますが、これで始めることができます。乾杯。

于 2012-05-23T14:13:33.823 に答える
2

Anthony Eden のトーク"Build and Test APIs with Ruby and Cucumber" をご覧ください。

于 2012-05-23T14:52:27.033 に答える
1

Cucumber(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 |
      | test@example.com | blabla   |
    When the user requests POST /users
    Then the response should be JSON:
    """
    [
      {"email": "test@example.com", "username": "blabla"}
    ]
    """

次に、pickle gem が非常に役立つ ステップを記述するだけです。

于 2012-05-23T19:06:12.153 に答える