首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于新手:使用方法在模型中使用rspec进行测试

对于新手:使用方法在模型中使用rspec进行测试
EN

Stack Overflow用户
提问于 2013-07-23 15:20:05
回答 1查看 29关注 0票数 0

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

代码语言:javascript
复制
def current?
  self.start_date.present? && self.end_date.nil?
end

现在我想测试一下ownership.current?

我该怎么做呢?下面是我所尝试的,使用expect...

代码语言:javascript
复制
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

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

你知道怎么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-23 15:40:07

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17803767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档