重置数据库后,RSpec测试总是立即失败

时间:2016-02-18 23:24:55

标签: ruby-on-rails ruby rspec

我正在编写RSpec测试以确保我的数据库关联正常工作。我注意到在重置数据库后,我的测试总是失败。他们总是期待一个物体,但却取而代之的是零。我还注意到,在编写新测试后,新测试将立即失败。如果我再次运行测试套件并且测试开始时是正确的,那么它们都会通过。

如何在测试数据库重置后立即通过测试?

以下是一个示例测试文件:

    describe 'Player' do

        let(:clan) {Clan.first || Clan.create(name: "Test Clan")}
        let(:kingdom) {Kingdom.first || Kingdom.create}
        let(:player) {Player.first || Player.create(username: "Foo", uuid: "9b15dea6-606e-47a4-a241-420251703c59", clan_id: 1, clan_role: "member", kingdom_id: 1, kingdom_role: "pleb")}
        let(:ip_address) {IpAddress.first || IpAddress.create(ip: "55.55.555.55")}
        let(:punishment) {Punishment.first || Punishment.create(offender_id: 1)}
        let(:connection) {Connection.first || Connection.create(player_id: 1, ip_address_id: 1)}
        let(:bank_account) {BankAccount.first || BankAccount.create(account_owner_id: 1)}
        let(:sell_offer) {SellOffer.first || SellOffer.create(seller_bank_account_id: 1, item_id: 1)}
        let(:buy_offer) {BuyOffer.first || BuyOffer.create(buyer_bank_account_id: 1, item_id: 1)}
        let(:owned_item) {OwnedItem.first || OwnedItem.create(owner_bank_account_id: 1, item_id: 1)}

      context 'associations' do
        it 'has ip addresses' do
          expect(player.ip_addresses.first).to eq(ip_address)
        end
        it 'has connections' do
          expect(player.connections.first).to eq(connection)
        end
        it 'has punishments' do
          expect(player.punishments.first).to eq(punishment)
        end
        it 'has a clan' do
          expect(player.clan).to eq(clan)
        end
        it 'has a bank account' do
          expect(player.bank_account).to eq(bank_account)
        end
        it 'has sell offers' do
          expect(player.sell_offers.first).to eq(sell_offer)
        end
        it 'has buy offers' do
          expect(player.buy_offers.first).to eq(buy_offer)
        end
        it 'has owned items' do
          expect(player.owned_items.first).to eq(owned_item)
        end
        it 'has a kingdom' do
          expect(player.kingdom).to eq(kingdom)
        end
      end

      context 'class methods' do
        # stuff
      end
    end

0 个答案:

没有答案