testing - 使用 cucumber 测试 omniauth 登录时没有哈希错误的方法 uid

标签 testing twitter cucumber omniauth uid

我正在尝试编写涉及使用 Omniauth(特别是 Twitter)登录的集成测试。每当运行 Cucumber 测试时,我得到的错误是:

When I sign in with "Twitter" features/step_definitions/steps.rb:5 undefined method uid' for #<Hash:0x007fa891f95830> (NoMethodError) ./app/models/user.rb:26:infrom_omniauth' ./app/controllers/omniauth_callbacks_controller.rb:4:in all' ./features/step_definitions/steps.rb:6:in/^I sign in with "(.*?)"$/' features/user_management.feature:15:in `When I sign in with "Twitter"'

下面是我的用户模型的代码。

def self.from_omniauth(auth)
    user = where(twitter_uid: auth.uid).first || check_for_non_twitter_login(auth)
    user.twitter_oauth_token = auth.credentials.token
    user.twitter_oauth_secret = auth.credentials.secret
    user.save! if user.email != ""
    user
end

def self.create_from_omniauth(auth)
    create do |user|
      user.provider = auth.provider
      user.twitter_uid = auth.uid
      user.twitter_username = auth.info.nickname
      user.twitter_oauth_token = auth.credentials.token
      user.twitter_oauth_secret = auth.credentials.secret
    end
end

我尝试通过将有问题的行更改为来更改访问 uid 值的方式:

user = where(twitter_uid: auth['uid']).first || check_for_non_twitter_login(auth)

这消除了该错误,因此我更改了 auth 哈希中所有提及的数据以使用相同的方法。这破坏了代码在开发中的工作方式(它根本不起作用),然后测试抛出了这个错误:

When I sign in with "Twitter" features/step_definitions/steps.rb:5 undefined method []' for nil:NilClass (NoMethodError) ./app/models/user.rb:56:inblock in create_from_omniauth' ./app/models/user.rb:53:in create_from_omniauth' ./app/models/user.rb:38:incheck_for_non_twitter_login' ./app/models/user.rb:26:in from_omniauth' ./app/controllers/omniauth_callbacks_controller.rb:4:inall' ./features/step_definitions/steps.rb:6:in /^I sign in with "(.*?)"$/' features/user_management.feature:15:inWhen I sign in with "Twitter"'

有人知道我做错了什么吗?

最佳答案

找到了解决办法。而不是使用:

OmniAuth.config.mock_auth ...etc

我用过:

OmniAuth.config.add_mock(:twitter, {"uid" => '12345', "credentials" => {"token" => "mytoken","secret" => "mysecret"} })

现在一切正常。

关于testing - 使用 cucumber 测试 omniauth 登录时没有哈希错误的方法 uid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21117141/

相关文章:

python - 如何测试是否已导入一个 python 模块?

unit-testing - java.lang.NoClassDefFoundError : org/objenesis/ObjenesisStd with Mockito 错误

cucumber - 如何使用 Cucumber 重新运行失败的场景?

java - cucumber :输出文件夹中未生成范围报告

c - LoadRunner - 在 lr_eval_string 函数中使用 %d

android - 在当前 webview 中打开弹出/外部站点链接

c++ - twitcurl OAuth - 错误 89 : Invalid/Expired Token

php - 如何通过推特账号登录Yii2

cucumber - 如何使用 OR AND 多个标签运行 Cucumber

unit-testing - 这个设计(反)模式的名称是什么?