模型测试为我的has_and_belongs_to_many关系提供的数据超出预期

时间:2015-09-11 18:10:12

标签: ruby-on-rails ruby activerecord

我有一个可以拥有多家结算公司的小组。 我的计费公司可以有很多团体。所以我创建了一个连接表迁移,并在每个模型中添加了它:

group.rb:

var stringDate='26.6.2015 г.';// assuming this the format you get and used regex to match this pattern
stringDate=stringDate.substring(0, stringDate.indexOf(' '));// to remove everything after space including space
var pattern = /^(\d{1,2})\.(\d{1,2})\.(\d{4})$/;
var arrayDate = stringDate.match(pattern);
var dt = new Date(arrayDate[3], arrayDate[2] - 1, arrayDate[1]);
console.log(dt)// this is date object

和billing.rb:

class Group < ActiveRecord::Base

    #I have to figure out how providers will be held in this app if its going to be an intermediary table or what.  Does deleting a provider from a group remove them
    #from the db all together or just that group?

    #validations
    validates :group_name, presence: true


    #associations

    #i think my issue is here, I have a lot of providers through provider_locations but there is no way to tell a group that provider_locations and group_locations are linked.
    has_many :group_locations
    has_many :providers, through: :provider_locations
    has_many :provider_locations, through: :group_locations
    has_many :invoices
    has_and_belongs_to_many :billings
    has_many :addresses, through: :group_locations



    belongs_to  :billing_address, class_name: 'Address', foreign_key: 'billing_address_id'
    belongs_to  :mailing_address, class_name: 'Address', foreign_key: 'mailing_address_id'
    has_and_belongs_to_many :insurances
    #has_many :provider_locations, through: :group_locations


end

我认为我终于理解了协会,但在我的计费测试中。我将计费公司添加到两个不同的组,然后当我尝试获得所有计费公司时,它返回三个。它应该返回一个有两组的人。我不明白为什么会这样。我的测试代码在这里:

class Billing < ActiveRecord::Base
  belongs_to :address
  has_and_belongs_to_many :groups
end
编辑:添加了一些打印声明,不知道为什么这个吸盘启动时有2个账单存在。我可以删除所有这些,添加一个账单一切看起来都很好。重新运行测试,每次开始都有两个账单存在同样的事情。所以我也打印出第一张账单的名字。这是MyText。我使用代理搜索来搜索我的项目文件夹,因为它看起来很多。即在billings.yml fixture中。

  require 'test_helper'


class BillingTest < ActiveSupport::TestCase

    def setup


        puts Billing.delete_all  #shows 2
        puts 'First count'
        puts Billing.all.count #shows 0
        puts 'End of count'


        @group = Group.create(group_name: 'My Awesome', group_contact: 'pbs@myawesome.com', tin: '382FSJ2', npi: '2301329', notes: 'group was termed but then reinstated')
        adr = Address.create(city: 'Las Cruces', state: 'New Mexico', zip: '88012', address_line_one: '382 Dark Side of the Moon', address_line_two: '')
        gl =GroupLocation.create(group: @group, address: adr)
        adr2 = Address.create(city: 'Albuquerque', state: 'New Mexico', zip: '88012', address_line_one: '1310 Constitution NE', address_line_two: '')
        @group.mailing_address = adr2
        @provider = Provider.create(first_name: 'Shane', last_name: 'Thomas')
        pl = ProviderLocation.create(provider: @provider, group_location: gl)


         @group2 = Group.create(group_name: 'My Other Group', group_contact: 'pbs@myothergroup.com', tin: '312fsf', npi: '412512', notes: '')
        adr2 = Address.create(city: 'Las Cruces', state: 'New Mexico', zip: '88012', address_line_one: '381 Far Side of the Moon', address_line_two: '')

        gl2 =GroupLocation.create(group: @group2, address: adr2)
        adr3 = Address.create(city: 'Albuquerque', state: 'New Mexico', zip: '88012', address_line_one: '1310 Constitution NE', address_line_two: '')
        @group2.mailing_address = adr3
        @provider2 = Provider.create(first_name: 'Shane', last_name: 'Thomas')
        p2 = ProviderLocation.create(provider: @provider2, group_location: gl2)



        @billing = Billing.create(name: 'Marcha billing company', contact: 'bob@marcha.com')
        @billing.address = Address.create(city: 'Las Cruces', state: 'New Mexico', zip: '88012', address_line_one: '501 Search-Bloc Rd', address_line_two: '')

        @group.billings << @billing #add this billing to a group.
        @group2.billings << @billing

        puts 'Last count'
        puts Billing.all.count  #shows 1
        puts 'End of Last count'


    end

    test 'retreiving all the groups for a billing company' do
        puts Billing.all.count #shows 1
        Billing.all.each do | bill |
            if bill.groups.count > 0
                puts bill.groups.first.group_name
            end
        end 
    end


end

我猜灯具是一种让数据快速进行测试的方法。我使用它们有点紧张,因为我的很多并且属于manys使用连接表并且不知道这些灯具是如何工作的,因为ID必须匹配以进行一致的测试。看起来我可以在灯具中订购列表,但我想这样可行。

如果我想继续使用我的设置数据而不是固定装置,是否可以快速关闭它们?

0 个答案:

没有答案