如何为Devise编写测试;我的Rails 3应用程序中的Omniauth?

时间:2011-08-28 20:03:56

标签: ruby-on-rails ruby-on-rails-3 rspec devise omniauth

我有一个与Omniauth和Devise的Rails 3应用程序。我无法找到一个可靠的教程,展示我应该如何编写用于测试功能的规范。

我想要实现的是确保注册和登录表单正常工作......这意味着用户可以创建帐户。我还想确保Omniauth FB Connect在任何时候都能正常工作。

我为上述这些场景编写测试的内容是什么/如何?

由于

1 个答案:

答案 0 :(得分:0)

测试非常有用,可以测试我必须处理来自Yahoo,Facebook,Google和AOL的身份验证的非常复杂的方法。特别是边缘情况。

这应该让你开始,这是我10多个场景中的第一个。

.feature文件

 Feature: Signinin with third party service: Google, Aol, Facebook and Yahoo 
      In order to use the site
      As a User
      I want to sign in using my Facebook or Gmail accounts

      Before do
        OmniAuth.config.test_mode = true
      end

      After do
        OmniAuth.config.test_mode = false
      end


      Scenario Outline: Sign with valid names and emails
        Given that I have a valid "<provider>" account with email "<email>" and name "<name>" 
          And that I am not signed in
          And I go to the sign in page
        When I follow image link "<provider>"
        Then I should see "You signed in using your <provider> account ("
          And I should see "Welcome to Death Star"

        Examples: valid name and email variations
            | provider | email                     | name            |
            | Google   | lukeskywaler@gmail.com    | luke skywlaker  |
            | Facebook | darth.vader@aol.com       | darth vader     |

my_step.rb帮助:

Given /^that I have a valid "([^"]*)" account with email "([^"]*)" and name "([^"]*)"$/ do |provider, email, name|
  OmniAuth.config.test_mode = true
  if provider.downcase == "yahoo" or provider.downcase == "google" or provider.downcase == "aol"   
     # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
     OmniAuth.config.mock_auth[:open_id] = 
     {
         'provider'  => "#{provider}",
         'uid'       => "#{provider}.com",
         'user_info' => { 'email' => "#{email}", 'name' => "#{name}"}
     }
  else
     # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
     OmniAuth.config.mock_auth[:facebook] = {
       'provider' => 'facebook',
       'uid'      => "531564247",
       'credentials' => {
         'token'=> "18915faketoken22|2.NKt21XnznTNEDTTERDGYXI2UUw__.3600.1302234329200-531564247|Mi0DhWREl6g-T9bMZnL82u7s4MI"
         },
       'user_info' => { 
         'nickname'   => "profile.php?id=53232564247",
         'email'      => "#{email}",
         'first_name' => "Luke",
         'last_name'  => "Skywalker",
         'name'       => "Luke Skywalker",
         'image'      => "http://graph.facebook.com/5asd54247/picture?type=square",
         'urls'       => {
           'facebook' => "http://www.facebook.com/profile.php?id=5asd54247", 
           'website'  => ""
           }
       },
       'extra' => { 
         'user_hash' => { 
             'id'         => "5asd54247",
             'name'       => "#{name}",
             'first_name' => "#{name.split(' ')[0]}",
             'last_name'  => "#{name.split(' ')[1]}",
             'link'       => "http://www.facebook.com/profile.php?id=5asd54247",
             'birthday'   => "12/7/1932",
             'hometown'   => { 
               'id'   => "104048449631599",
               'name' => "Menlo Park, California"
             },
             'location' => { 
               'id'   => "104048449631599",
               'name' => "Menlo Park, California"
             },
             'gender'   => "male",
             'email'    => "#{email}",
             'timezone' => "-7",
             'locale'   => "en_US",
             'verified' => true
           }
         }
       }
  end
end

希望这有帮助。