你如何规范模型的“只读”行为?

时间:2022-03-31 20:03:59

For example, let's say I have a Question model, that has the boolean fields answered and closed. How would I test the behavior that a Question should be read only when marked as answered using RSpec? This seems like it's the behavior of the model, but I'm not sure how to best test it. Should I be using a before filter for this behavior, and adding an error saying you can't modify an answered question? Or is there a better way to do it? I'm only learning RSpec and BDD.

例如,假设我有一个问题模型,它具有已回答和关闭的布尔字段。我如何测试只有在使用RSpec标记为已回答时才应读取问题的行为?这似乎是模型的行为,但我不确定如何最好地测试它。我是否应该使用前置过滤器来解决此问题,并添加错误,说明您无法修改已回答的问题?或者有更好的方法吗?我只学习RSpec和BDD。

1 个解决方案

#1


depends how you need it work, but...

取决于你需要它如何工作,但......

describe Question do
  it "should be read only when marked as answered" do
    question = Question.new(:title => 'old title')
    question.answered = true
    question.save

    # this
    lambda {
      question.title = 'new title'
    }.should raise_error(ReadOnlyError)

    # or
    question.title = 'new title'
    question.save
    question.title.should == 'old title'

    # or
    quesiton.title = 'new title'
    question.save.should be_false
  end
end

Or perhaps you want the error to be raised on save? Or maybe there is no error and it just silently doesn't change the value? It's up to you how you want to implement it, but the methodology is the same.

或者您可能希望在保存时引发错误?或者也许没有错误,它只是默默地不改变价值?这取决于您希望如何实现它,但方法是相同的。

  1. Setup your objects in the state you want to spec them
  2. 在要指定它们的状态下设置对象

  3. Make sure your objects in that state do what you expect
  4. 确保处于该状态的对象符合您的预期

So setup an answered question, and then see if you can change one its data. If you can't, then spec passed. It's up to you how you want the behaviour of your model to work. And the great thing about BDD is you think about this interface first, since you have to actually use an objects API in order to spec it out.

因此,设置一个已回答的问题,然后查看是否可以更改其数据。如果你不能,那么规范就通过了。这取决于您希望模型的行为如何工作。关于BDD的好处是你首先考虑这个接口,因为你必须实际使用对象API才能指出它。

#1


depends how you need it work, but...

取决于你需要它如何工作,但......

describe Question do
  it "should be read only when marked as answered" do
    question = Question.new(:title => 'old title')
    question.answered = true
    question.save

    # this
    lambda {
      question.title = 'new title'
    }.should raise_error(ReadOnlyError)

    # or
    question.title = 'new title'
    question.save
    question.title.should == 'old title'

    # or
    quesiton.title = 'new title'
    question.save.should be_false
  end
end

Or perhaps you want the error to be raised on save? Or maybe there is no error and it just silently doesn't change the value? It's up to you how you want to implement it, but the methodology is the same.

或者您可能希望在保存时引发错误?或者也许没有错误,它只是默默地不改变价值?这取决于您希望如何实现它,但方法是相同的。

  1. Setup your objects in the state you want to spec them
  2. 在要指定它们的状态下设置对象

  3. Make sure your objects in that state do what you expect
  4. 确保处于该状态的对象符合您的预期

So setup an answered question, and then see if you can change one its data. If you can't, then spec passed. It's up to you how you want the behaviour of your model to work. And the great thing about BDD is you think about this interface first, since you have to actually use an objects API in order to spec it out.

因此,设置一个已回答的问题,然后查看是否可以更改其数据。如果你不能,那么规范就通过了。这取决于您希望模型的行为如何工作。关于BDD的好处是你首先考虑这个接口,因为你必须实际使用对象API才能指出它。