Reputation: 1590
I have following code to test action create of controller
let(:custom_action) { create :custom_action, entity: entity }
Describe '#create' do
context 'with valid attributes' do
before { allow(controller).to receive(:custom_actions_path).and_return('/') }
subject { post :create, params: { custom_action: {
name: custom_action.name,
label: custom_action.label,
url: custom_action.url,
request_method: custom_action.request_method,
entity_id: entity.id
},
locale: user.language }}
it 'should increment resource list by 1' do
expect { subject }.to change { CustomAction.count }.by(1)
end
end
end
When i running test i get: To have changed by 1, but was changed by 2
I checked action, if create new object always create one, not two.
Have I correct used subject? What wrong with my test? Thank you
Upvotes: 1
Views: 530
Reputation: 5313
Calling subject
within your expect
block evaluates the subject
block. Subject block calls your action, but it needs to evaluate custom_action
to do so. In the end, while evaluating the block, your custom_action
block creates one CustomAction
and your actual, controller action creates one more during the request.
Change your let
to let!
to fix your test.
Upvotes: 3