有两个代码块正在执行。传递给的代码块
expect
,以及传递给
change
. 这才是真正发生的,在伪代码中。
difference = 1
initial_count = Article.count
article = Article.new(title: 'test', description: 'test')
article.save
final_count = Article.count
expect(final_count - initial_count).to eq(difference)
我将重构您的测试,使其更易于执行以下操作:
require 'rails_helper'
describe Article, type: :model do
let(:create_article) { Article.create(title: 'test', description: 'test') }
it 'is valid if title and description fields have value' do
expect { create_article }.to change { Article.count }.by(1)
end
end