Home How to test my devise code using rspec?
Reply: 1

How to test my devise code using rspec?

kethianu Published in 2018-02-14 04:10:07Z

Here is my unit testing code for devise. Are there any ways to write better test code?


require 'rails_helper'

    RSpec.describe User, type: :model do

      it { should have_many(:companies) }
      it { should have_many(:jobs) }
      it do
               should validate_length_of(:encrypted_password).

      it { is_expected.to validate_presence_of(:email) }
      it { is_expected.to validate_presence_of(:encrypted_password) }
      it { should have_db_column(:id) }
      it { should have_db_column(:email) }
      it { should have_db_column(:encrypted_password) }
      it { should have_db_column(:confirmation_token) }
      it { should have_db_column(:confirmed_at) }
      it { should have_db_column(:confirmation_sent_at) }

      it 'is databse authenticable' do
        user = User.create(
           email: 'test@example.com', 
          password: 'password123',
          password_confirmation: 'password123'
        expect(user.valid_password?('password123')).to be_truthy
Josh McMillan
Josh McMillan Reply to 2018-02-14 04:21:27Z

Okay – as I understand it you're asking for what the best practices are for testing a Rails model. The fact that it's auto-generated by Devise is largely irrelevant.

The first thing I would consider here is what it is that you're actually testing – most of your code here is testing the underlying implementation of Devise. That shouldn't be the responsibility of your application's test suite – the library has a bunch of tests that should test that for you.

The only real useful tests here, IMO, are the association tests (which look like they come from shoulda-matchers). The rest of the assertions here are testing Devise-specific code, which is tightly coupling your test suite to a 3rd-party library – that's only going to put you in a world of pain further down the line.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331573 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO