I have two Mongoid models: User and EmailAccounts. The latter is embedded in the User model. That configuration should be fine because it works generally.
Now I’m trying to write an integration test for my user edit form that looks like this:
describe 'Add EmailAccount' do
it 'Adds an email account', js: true do
user = FactoryGirl.create(:user_without_email_accounts)
visit edit_user_path(user)
expect{
click_link 'New Email Account'
within '.nested-fields' do
fill_in 'Account Name', with: 'New Email Account'
fill_in 'Other Field', with: 'Other Data'
end
click_button 'Save'
}.to change(EmailAccount, :count).by(1)
end
end
Because EmailAccount is an embedded model the change of count is always 0.
Can I check for a change of the EmailAccount counter in any similar way? Or do I have to go a different way?
This won’t work neither:
}.to change(user.email_accounts, :count).by(1)
I had the exact same issue and managed to solve it by using a combination the answers posted here.