Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RobBikmansurov/c456524d00099ba14a25b6ac24c4a9d9 to your computer and use it in GitHub Desktop.
Save RobBikmansurov/c456524d00099ba14a25b6ac24c4a9d9 to your computer and use it in GitHub Desktop.

Revisions

  1. @alex-zige alex-zige revised this gist Jun 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ describe "API authentication" , :type => :api do

    end

    ```ruby
    ```

    ## ``ActionController::TestCase::Behavior`` v.s. ``Rack::Test::Methods``

  2. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Rspec APIs Testing Notes
    # Rails Rspec APIs Testing Notes
    ## Folders Structure
    ```
    spec
  3. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ end
    *Notes, if you change your spec/apis folder to api, you will find out the ``last_repsonse`` is not working any more. That's because the :type=> :api scope got mixed with default ActionController behavior.


    # A Setp Further
    # A Step Further

    ## Reusable Sign in Helper.
    ```ruby
  4. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Respec APIs Testing Notes
    # Rspec APIs Testing Notes
    ## Folders Structure
    ```
    spec
  5. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,13 @@
    |--- your_api_test_spec.rb
    |--- controllers
    |--- models
    |--- factories
    |--- views
    |--- helpers
    |--- supports
    |--- api_helper.rb
    |--- authentication_helper.rb
    |--- spec_helper.rb
    ```
    *note: do not put your apis folder underneath controllers folder, otherwise, it will inherited with controller ``ActionController::TestCase::Behavior``, ``Rake::Test::Methods`` cannot be apply accordingly.

    @@ -32,14 +36,14 @@ end

    ```
    ## Enable Spec_helper supports
    ```
    ```ruby
    @spec/spec_helper.rb
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
    ```

    ## API Test Spec
    make sure your sample test group has type: api, then it will include Rake::Test::Methods for (``get,post,put,delete``) requests
    ```
    ```ruby
    @spec/apis/authentication_spec.rb

    require "spec_helper"
    @@ -57,7 +61,7 @@ describe "API authentication" , :type => :api do

    end

    ```
    ```ruby
    ## ``ActionController::TestCase::Behavior`` v.s. ``Rack::Test::Methods``
  6. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 45 additions and 0 deletions.
    45 changes: 45 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -88,3 +88,48 @@ end
    * ``Rake::Test::Methods`` could query the url "/api/v1/xxxx", but ``ActionController::TestCase::Behavior`` only execute actions within current controller scope.
    * the reponse in ``Rake::Test::Methods`` called: last_response, in ``ActionController::TestCase::Behavior`` called: response.

    *Notes, if you change your spec/apis folder to api, you will find out the ``last_repsonse`` is not working any more. That's because the :type=> :api scope got mixed with default ActionController behavior.


    # A Setp Further

    ## Reusable Sign in Helper.
    ```ruby
    @spec/supports/authentication_helper.rb

    module AuthenticationHelper
    def sign_in_as_a_valid_user
    @user ||= FactoryGirl.create(:user)
    @user.reset_authentication_token! unless @user.authentication_token
    set_cookie "authentication_token=#{@user.authentication_token}"
    end
    end

    RSpec.configure do |config|
    config.include AuthenticationHelper, :type=>:api
    end
    ```

    *Notes: The Example above used token based cookie authentication. So you could change to base-authen or token-based auth based on your own needs.

    ## Created a signed in user in your Test
    use before_each or before_all block or include the helper method into your assertion block
    ```ruby
    require "spec_helper"

    describe "API Items Controller", :type => :api do

    before :each do
    sign_in_as_a_valid_user
    end

    it "fetch all items" do
    #or include the helper method here
    sign_in_as_a_valid_user
    ...
    end
    end

    ```


  7. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 4 additions and 10 deletions.
    14 changes: 4 additions & 10 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -59,16 +59,10 @@ end
    ```

    ## ActionController::TestCase::Behavior vs Rack::Test::Methods

    ``
    ActionController::TestCase::Behavior
    ``
    v.s.
    ``
    Rack::Test::Methods
    ``
    Rspec Rails includes ``ActionController::TestCase::Behavior`` for simulating controller requests.
    ## ``ActionController::TestCase::Behavior`` v.s. ``Rack::Test::Methods``

    Rspec-Rails includes ``ActionController::TestCase::Behavior`` for simulating controller requests.

    so you could do

    ```ruby
  8. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Respec APIs Testing Notes
    ## Folder Structure
    ## Folders Structure
    ```
    spec
    |--- apis #do not put into controllers folder.
    @@ -59,11 +59,11 @@ end
    ```

    ##Interesting points.
    ## ActionController::TestCase::Behavior vs Rack::Test::Methods

    ``
    ActionController::TestCase::Behavior
    ``
    ``
    v.s.
    ``
    Rack::Test::Methods
    @@ -90,7 +90,7 @@ end

    ```

    Two points of differents:
    ### Points of differents:
    * ``Rake::Test::Methods`` could query the url "/api/v1/xxxx", but ``ActionController::TestCase::Behavior`` only execute actions within current controller scope.
    * the reponse in ``Rake::Test::Methods`` called: last_response, in ``ActionController::TestCase::Behavior`` called: response.

  9. @alex-zige alex-zige revised this gist Jun 18, 2013. No changes.
  10. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 61 additions and 7 deletions.
    68 changes: 61 additions & 7 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,20 @@
    ## Folder Structure
    ```
    spec
    |--- apis
    |--- your_api_test_spec.rb
    |--- apis #do not put into controllers folder.
    |--- your_api_test_spec.rb
    |--- controllers
    |--- models
    |--- supports
    |--- api_helper.rb
    |--- authentication_helper.rb
    ```
    *note: do not put your apis folder underneath controllers folder, otherwise, it will inherited with controller ``ActionController::TestCase::Behavior``, ``Rake::Test::Methods`` cannot be apply accordingly.

    ## Custom Rspec Helper for Rake::Test::Methods for api scopes.

    ```ruby
    spec/supports/api_helper.rb
    @spec/supports/api_helper.rb


    module ApiHelper
    @@ -29,14 +30,67 @@ RSpec.configure do |config|
    config.include ApiHelper, :type=>:api #apply to all spec for apis folder
    end

    ```
    ## Enable Spec_helper supports
    ```
    @spec/spec_helper.rb
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
    ```

    ## API Test Spec
    make sure your sample test group has type: api, then it will include Rake::Test::Methods for (``get,post,put,delete``) requests
    ```
    @spec/apis/authentication_spec.rb
    require "spec_helper"
    ```ruby
    ActionController::TestCase::Behavior
    describe "API authentication" , :type => :api do
    v.s.
    let!(:user) { FactoryGirl.create(:user) }
    it "making a request without cookie token " do
    get "/api/v1/items/1",:formate =>:json
    last_response.status.should eql(401)
    error = {:error=>'You need to sign in or sign up before continuing.'}
    last_response.body.should eql(error.to_json)
    end
    end
    ```

    ##Interesting points.

    ``
    ActionController::TestCase::Behavior
    ``
    v.s.
    ``
    Rack::Test::Methods
    ```
    ``
    Rspec Rails includes ``ActionController::TestCase::Behavior`` for simulating controller requests.
    so you could do

    ```ruby

    require "spec_helper"

    describe Api::V1::SessionsController , :type => :api do

    let!(:user) { FactoryGirl.create(:user) }

    it "making a request without cookie token " do
    get :index
    response.status.should eql(401)
    error = {:error=>'You need to sign in or sign up before continuing.'}
    response.body.should eql(error.to_json)
    end

    end

    ```

    Two points of differents:
    * ``Rake::Test::Methods`` could query the url "/api/v1/xxxx", but ``ActionController::TestCase::Behavior`` only execute actions within current controller scope.
    * the reponse in ``Rake::Test::Methods`` called: last_response, in ``ActionController::TestCase::Behavior`` called: response.

  11. @alex-zige alex-zige revised this gist Jun 18, 2013. No changes.
  12. @alex-zige alex-zige revised this gist Jun 18, 2013. No changes.
  13. @alex-zige alex-zige revised this gist Jun 18, 2013. No changes.
  14. @alex-zige alex-zige revised this gist Jun 18, 2013. No changes.
  15. @alex-zige alex-zige revised this gist Jun 18, 2013. No changes.
  16. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,10 @@
    |--- models
    |--- supports
    |--- api_helper.rb
    |--- authentication_helper.rb
    ```

    ## Custom Rspec Helper for Rake::Test::Methods.
    ## Custom Rspec Helper for Rake::Test::Methods for api scopes.

    ```ruby
    spec/supports/api_helper.rb
    @@ -25,7 +26,7 @@ module ApiHelper
    end

    RSpec.configure do |config|
    config.include ApiHelper, :type=>:api
    config.include ApiHelper, :type=>:api #apply to all spec for apis folder
    end


  17. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    #h1 Respec APIs Testing Notes
    # Respec APIs Testing Notes
    ## Folder Structure
    ```
    spec
    |--- apis
    |--- your_api_test_spec.rb
    |--- controllers
    |--- models
    |--- supports
    |--- api_helper.rb
    ```

    ## Custom Rspec Helper for Rake::Test::Methods.

  18. @alex-zige alex-zige revised this gist Jun 18, 2013. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,35 @@
    #h1 Respec APIs Testing Notes
    ## Folder Structure
    spec
    |--- apis
    |--- your_api_test_spec.rb
    |--- controllers
    |--- models
    |--- supports
    |--- api_helper.rb

    ## Custom Rspec Helper for Rake::Test::Methods.

    ```ruby
    spec/supports/api_helper.rb


    module ApiHelper
    include Rack::Test::Methods

    def app
    Rails.application
    end
    end

    RSpec.configure do |config|
    config.include ApiHelper, :type=>:api
    end


    ```


    ```ruby
    ActionController::TestCase::Behavior

  19. @alex-zige alex-zige renamed this gist Jun 17, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.txt → gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    ```ruby
    ActionController::TestCase::Behavior

  20. @alex-zige alex-zige created this gist Jun 17, 2013.
    8 changes: 8 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@

    ```ruby
    ActionController::TestCase::Behavior

    v.s.

    Rack::Test::Methods
    ```