-
-
Save RobBikmansurov/c456524d00099ba14a25b6ac24c4a9d9 to your computer and use it in GitHub Desktop.
Revisions
-
alex-zige revised this gist
Jun 23, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,7 +61,7 @@ describe "API authentication" , :type => :api do end ``` ## ``ActionController::TestCase::Behavior`` v.s. ``Rack::Test::Methods`` -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Rails Rspec APIs Testing Notes ## Folders Structure ``` spec -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 Step Further ## Reusable Sign in Helper. ```ruby -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Rspec APIs Testing Notes ## Folders Structure ``` spec -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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`` -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 45 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 4 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,16 +59,10 @@ end ``` ## ``ActionController::TestCase::Behavior`` v.s. ``Rack::Test::Methods`` Rspec-Rails includes ``ActionController::TestCase::Behavior`` for simulating controller requests. so you could do ```ruby -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ # Respec APIs Testing Notes ## Folders Structure ``` spec |--- apis #do not put into controllers folder. @@ -59,11 +59,11 @@ end ``` ## ActionController::TestCase::Behavior vs Rack::Test::Methods `` ActionController::TestCase::Behavior `` v.s. `` Rack::Test::Methods @@ -90,7 +90,7 @@ end ``` ### 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. -
alex-zige revised this gist
Jun 18, 2013 . No changes.There are no files selected for viewing
-
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 61 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,19 +2,20 @@ ## Folder Structure ``` spec |--- 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 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" describe "API authentication" , :type => :api do 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. -
alex-zige revised this gist
Jun 18, 2013 . No changes.There are no files selected for viewing
-
alex-zige revised this gist
Jun 18, 2013 . No changes.There are no files selected for viewing
-
alex-zige revised this gist
Jun 18, 2013 . No changes.There are no files selected for viewing
-
alex-zige revised this gist
Jun 18, 2013 . No changes.There are no files selected for viewing
-
alex-zige revised this gist
Jun 18, 2013 . No changes.There are no files selected for viewing
-
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 #apply to all spec for apis folder end -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,14 @@ # 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. -
alex-zige revised this gist
Jun 18, 2013 . 1 changed file with 32 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
alex-zige renamed this gist
Jun 17, 2013 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ ```ruby ActionController::TestCase::Behavior -
alex-zige created this gist
Jun 17, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ ```ruby ActionController::TestCase::Behavior v.s. Rack::Test::Methods ```