首页 > 代码库 > rspec subject{}和it{}

rspec subject{}和it{}

subject { build(:user,firstname:john,lastname:doe)}it { should be_named john doe}

it{}和subject{}互为别名

Gem扩展代码库 shoulda-matchers

直接使用

subject { Contact.new }sperify { should validate_presence_of :firstname } 

自定义匹配器

Rspec::Matchers.define :be_named do |expected|    match do |actual|        actual.name eq expected    end    description do         "return a full name as a string"    endend#use the defined matchersit { should be_named John Doe}

 

rspec subject{}和it{}