【问题标题】:HowTo Write tests for Devise & Omniauth in my Rails 3 app?如何在我的 Rails 3 应用程序中为 Devise 和 Omniauth 编写测试?
【发布时间】:2011-08-28 20:03:55
【问题描述】:

我有一个带有 Omniauth 和 Devise 的 Rails 3 应用程序。我还没有找到一个可靠的教程来展示我应该如何编写规范来测试功能。

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

我如何/如何为上述这些场景编写测试?

谢谢

【问题讨论】:

  • 有人吗?有什么想法吗?你的测试是什么样的?

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


【解决方案1】:

这些测试对于测试我必须处理来自 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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    相关资源
    最近更新 更多