ruby-on-rails - Rspec 和 Factory Girl 的 Rails 问题

标签 ruby-on-rails rspec factory-bot

我正在尝试使用 Rails 解决问题。这是我的第一个 Rails 应用程序,我正在评估我们 future 项目的过程中对其进行测试。我一直关注 Railstutorial.org 直到第 9 章,然后尝试自己继续。

使用 Rails 3.2.3、Ruby 1.9.3、Factory Girl 1.4.0 和 rspec 2.10.0。

我遇到的麻烦是客户端--[has_many]-->用户关系。

运行测试时无法解决的错误:

1) User 
     Failure/Error: let(:client) { FactoryGirl.create(:client) }
     NoMethodError:
       undefined method `user' for #<Client:0x000000045cbfa8>

规范/工厂.rb

FactoryGirl.define do
  factory :client do
    sequence(:company_name)  { |n| "Company #{n}" }
    sequence(:address) { |n| "#{n} Example Street"}   
    phone "0-123-456-7890"
  end

  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}   
    password "foobar"
    password_confirmation "foobar"
    client

    factory :admin do
      admin true
    end
  end

规范/模型/user_spec.rb

require 'spec_helper'

describe User do

  let(:client) { FactoryGirl.create(:client) }
  before { @user = client.users.build(name: "Example User", 
                        email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b1e180e192b0e130a061b070e45080406" rel="noreferrer noopener nofollow">[email protected]</a>", 
                        password: "foobar", 
                        password_confirmation: "foobar") }

  subject { @user }

  it { should respond_to(:name) }
end

应用程序/ Controller /clients_controller.rb

class ClientsController < ApplicationController
  def show
    @client = Client.find(params[:id])
  end

  def new
    @client = Client.new
    @client.users.build # Initializes an empty user to be used on new form
  end

  def create
    @client = Client.new(params[:client])
    if @client.save
      flash[:success] = "Welcome!"
      redirect_to @client
    else
      render 'new'
    end
  end
end

应用程序/ Controller /user_controller.rb

class UsersController < ApplicationController
  .
  .
  .

  def new
     @user = User.new
  end

  .
  .
  .
end

应用程序/模型/user.rb

class User < ActiveRecord::Base
  belongs_to :client

  .
  .
  .
end

app/models/client.rb

class Client < ActiveRecord::Base 
  has_many :users, dependent: :destroy
  .
  .
  .

end

感谢您的帮助!

最佳答案

在您的 user_spec 中,您正在调用 client.users,但它在其他地方显示 client 属于 user (单数)。如果是这样,请尝试以下操作:

FactoryGirl.define do
  factory :client do
    ...
    association :user
  end
end

describe User do
   let(:user) { FactoryGirl( ... ) }
   let(:client) { FactoryGirl(:client, :user => user) }
   subject { user }
   it { should respond_to(:name) }
end

关于ruby-on-rails - Rspec 和 Factory Girl 的 Rails 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10948821/

相关文章:

ruby-on-rails - ActiveRecord 在简单创建时引发重复键错误

ruby-on-rails - Rails 5 地址已在使用中 - bind(2) for "127.0.0.1"port 3000

ruby-on-rails - 机械师,我如何引用我正在制作的对象并将其传递给关联? (关联类型不匹配)

ruby-on-rails - 在 Rails 上使用 'rinruby' gem 时,我目前无法使用 png ("path/to/file.png") & dev.off() 将热图等转储到文件夹中

ruby-on-rails - ArgumentError : An SMTP To address is required to send a message. 设置消息 smtp_envelope_to、to、cc 或 bcc 地址

ruby-on-rails - RSpec - 测试强参数 ActionController::ParameterMissing

ruby-on-rails - 为什么 rspec 不使用密码重置 token 更新数据库记录?

ruby-on-rails - Rspec 没有在 Linux 中加载 ControllerMacros 模块(但在 OS X 上很好)

ruby-on-rails - Rails FactoryGirl 错误 "undefined method ` 生成 LoremIpsum :Module (NoMethodError)"

ruby-on-rails - 如何在 rails 开发环境中使用 factory_girl 正确排序?