ruby-on-rails-3 - 如何使用 rspec 和 factory girl 设置我的身份验证数据?

标签 ruby-on-rails-3 authentication testing rspec factory-bot

我有一个简单的用户工厂,如下所示:

FactoryGirl.define do
  factory :user do
    name "jeff"
    email "jeff@lint.com"
    password "foobar"
    password_confirmation "foobar"
  end
end

我正在尝试像这样测试内置的 authenticate 方法:

  describe "return value of authenticate method", focus: true do

    before do
      create(:user)
    end

    let(:found_user) { User.find_by_email(:email) }

    it "can return value of authenticate method" do
      expect(:user).to eq found_user.authenticate(:password)
    end

  end

我得到的错误是

NoMethodError:
       undefined method `authenticate' for nil:NilClass

这可能意味着 found_user 正在返回 nil。但我不明白为什么。当我在控制台上尝试这段代码时,它工作得很好。那我做错了什么?我对 Factory Girl 很陌生。

我也在寻找的是在不使用实例变量的情况下做到这一点。

最佳答案

试试这个

describe "return value of authenticate method", focus: true do

   before do
     @user = FactoryGirl.create(:user)
   end

   let(:found_user) { User.find_by_email(@user.email) }

   it "can return value of authenticate method" do  
     expect(@user).to eq found_user.authenticate(@user.password)
   end
end

关于ruby-on-rails-3 - 如何使用 rspec 和 factory girl 设置我的身份验证数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417273/

相关文章:

ruby-on-rails - 嵌套属性无法使用新父级创建子级

ruby-on-rails - Geocoder Gem - 获取 request.location.country 的 "Reserved"(即使在本地!)

node.js - Express应用程序的简单密码验证

testing - 如何测试两个用户之间的异步网站事件

ruby-on-rails - 在 RSpec 中编写请求规范的正确方法是什么?

security - 记住我 - 持续时间应该有多长

iphone - 安全地向服务器验证 iPhone 应用程序?

testing - JMeter 无法使用 JMS 订阅者读取消息,给我 404 状态代码作为响应

javascript - 排除嵌套目录的 karma 配置

ruby-on-rails - 如何在 Rails Controller 测试中断言响应代码?