0

我最近将一个项目从 MongoDB/mongoid 转换为 PostgreSQL。过渡后,我的所有模型规格都通过了,并且大多数控制器规格都通过了。我的三个控制器没有通过测试。

根据以下模型定义,警报、订单和用户模型取决于客户和债务人模型:

class Client < ActiveRecord::Base
  has_many :users, autosave: true, dependent: :destroy
  has_many :alerts, autosave: true, dependent: :destroy
  has_many :orders, autosave: true, dependent: :destroy

  ...
end

class Debtor < ActiveRecord::Base
  has_many :orders, autosave: true
  has_many :alerts, autosave: true

  ...
end

class Alert < ActiveRecord::Base
  belongs_to :client
  belongs_to :debtor

  ...
end

class Order < ActiveRecord::Base
  belongs_to :client
  belongs_to :user
  belongs_to :debtor

  ...
end

class User < ActiveRecord::Base
  rolify

  devise :database_authenticatable, 
     :recoverable, :rememberable, :trackable, :validatable

  has_and_belongs_to_many :roles, :join_table => :users_roles
  belongs_to :client
  has_many :orders
end

几乎所有失败的测试都有相同的错误(有些有“找不到带有 id= 的客户端”):

 129) OrdersController logged in as a regular user GET #new renders the new template
 Failure/Error: get :new, client_id: @user.client_id
 ActiveRecord::RecordNotFound:
   Couldn't find Debtor with id=
 # ./spec/controllers/orders_controller_spec.rb:59:in `block (4 levels) in <top (required)>'

我已经运行了 db:test:prepare 和 drop、create、migrate 序列。

更新:这是来自 orders_controller_spec.rb 和相关控制器的相关规范。我是一个 rspec 菜鸟,所以如果before(:each)封锁是反人类罪,我现在道歉。

require 'spec_helper'

describe OrdersController do
  before (:each) do
    @user, @user2, @user3, @admin = FactoryGirl.create(:user), FactoryGirl.create(:user), FactoryGirl.create(:user), FactoryGirl.create(:user)
    @client, @client2 = FactoryGirl.create(:client), FactoryGirl.create(:client)
    @debtor, @debtor2, @debtor3 = FactoryGirl.create(:debtor), FactoryGirl.create(:debtor), FactoryGirl.create(:debtor)
    @order, @order2, @order3, @order4 = FactoryGirl.create(:order), FactoryGirl.create(:order), FactoryGirl.create(:order),FactoryGirl.create(:order)
    @role = FactoryGirl.create(:role)
    @admin_role = FactoryGirl.create(:admin_role)
    @vip_role = FactoryGirl.create(:vip_role)
    @user.roles << @role
    @user2.roles << @vip_role
    @user3.roles << @role
    @admin.roles << @admin_role
    @client.users << @user << @admin
    @client.orders << @order << @order2
    @client2.users << @user2 << @user3
    @client2.orders << @order3 << @order4
  end

  context "logged in as a regular user" do
    before { sign_in @user }

    describe "GET #index" do
      before { get :index, client_id: @user.client_id }

      it "assigns a list of orders belonging to user client" do
        assigns(:orders).to_a.should eq([@order, @order2])
      end
      it "renders the :index template" do
        response.should render_template :index, client_id: @user.client_id
      end
    end

    describe "GET #show" do
      context "for order belonging to user client" do
        before { get :show, client_id: @user.client_id, id: @order }

        it "assigns the requested order to @order" do
          assigns(:order).should eq(@order)
        end
        it "renders the :show template" do
          response.should render_template :show, client_id: @user.client_id, id: @order
        end
      end

      context "for order not belonging to user client" do
        before { get :show, client_id: @client2.id, id: @order3 }

        it "redirects to the home view" do
          response.should redirect_to root_url
        end
      end 
    end

    describe "GET #new" do
      it "renders the new template" do
        get :new, client_id: @user.client_id
        response.should render_template :new, client_id: @user.client_id
      end
    end

    ...
  end
end

相关的控制器动作:

class OrdersController < ApplicationController
  before_filter :authenticate_user!
  before_filter :load_client
  before_filter :has_enough_units, :only => [:create]
  respond_to :html
  load_and_authorize_resource :debtor
  load_and_authorize_resource :order, :through => :client, :shallow => true

  def index
    if @client == nil
      @orders = Order.all
    else
      @orders = @client.orders.all
    end
    respond_with(@orders)
  end

  def show
    if @client == nil
      @order = Order.find(params[:id])
    else
      @order = @client.orders.find(params[:id])
    end
  end

  def new
    if current_user.has_role? :admin
      @order = Order.new
    else
      @order = @client.orders.build
    end
    @order.build_debtor
    @clients = Client.all.to_a
  end

  ...
end

希望这是足够的信息......

4

0 回答 0