0

我有一个Ownership模型,它有一个start_date和一个end_date,在我的模型中,我有以下代码:

def current?
  self.start_date.present? && self.end_date.nil?
end

现在我想测试ownership.current?

我该怎么做?这是我尝试过的,使用期望...

describe "when owning and giving date are nil" do
  before do
    @ownership.agreed = true
    @ownership.save
    @ownership.update_attributes(start_date: nil, end_date: nil)
  end
  it { should be_valid }
  expect(@ownership.current?).to be_false

  describe "then product is owned" do
    before { @ownership.update_attributes(start_date: 1.day.ago) }

    it { should be_valid }
    expect(@ownership.current?).to be_true

    describe "then product is given" do
      before { @ownership.update_attributes(end_date: 1.hour.ago) }

      it { should be_valid }
      expect(@ownership.current?).to be_false
    end
  end
end

......但它没有奏效。我试图用 替换expect(@ownership.current?).to be_false@ownership.current? { should be_false }但也没有成功。

你知道怎么做吗?

4

1 回答 1

0

我只测试当前的两个结果?

describe OwnerShip do
  it "#current? returns false" do
    ownership.current?.should be_false
  end

  context "when started and not yet ended" do
    it "#current? returns true" do
      ownership.start_date = Date.today
      ownership.end_date = nil
      ownership.current?.should be_true
    end
  end
end
于 2013-07-23T07:40:06.670 回答