Rspec的describe的参数和语法


RSpec.describe Link, "validations" do
  it { is_expected.to validate_presence_of(:title) }
  it { is_expected.to validate_presence_of(:url) }
  it { is_expected.to validate_uniqueness_of(:url) }
end
is_expected is an RSpec method that makes it easier to write one line tests.
The it these tests refer to is the test’s subject , a method provided by RSpec when you
pass a class as the first argument to describe .
subject 是当你传给describe的第一个函数是为class. subject 又可以看作是 it

RSpec takes the subject you pass into describe , and instantiates a new object.
Rspec把你带进来的subject实例化一个新对象。
In this case, subject returns Link.new .
is_expected is a convenience syntax for expect(subject) . 
is_expected只是一个简便的语法=expect(subject)

It reads a bit nicer when you read the whole line with the it . The following lines are roughly equivalent:

RSpec.describe Link, "validations" do
  it { expect(Link.new).to validate_presence_of(:title) }
  it { expect(subject).to validate_presence_of(:url) }
  it { is_expected.to validate_uniqueness_of(:url) }
end
阅读量: 534
发布于:
修改于: