Using RSpec with Rails Engine

12 views
Skip to first unread message

josep...@gmail.com

unread,
Feb 15, 2021, 4:21:55 PMFeb 15
to rspec
I am trying to create a Rails engine using RSpec for testing and I am following guides like this:


I have followed the guide up to the point where RSpec is set up but not Factory Bot (i.e., stopping after "Step 5: Get RSpec to load the Rails environment of the dummy app") and everything is working OK. I have put this in a Github repository here: https://github.com/jrmhaig/sample_engine

However, when I try to create a second model I am consistently getting "pending migration" errors from RSpec:

```
sample_engine$ bundle exec rails db:drop db:create db:migrate RAILS_ENV=test
Dropped database 'db/test.sqlite3'
Created database 'db/test.sqlite3'
== 20210215210420 CreateSampleEngineWidgets: migrating ========================
-- create_table(:sample_engine_widgets)
   -> 0.0013s
== 20210215210420 CreateSampleEngineWidgets: migrated (0.0014s) ===============

== 20210215210756 CreateSampleEngineGrommets: migrating =======================
-- create_table(:sample_engine_grommets)
   -> 0.0013s
== 20210215210756 CreateSampleEngineGrommets: migrated (0.0013s) ==============

sample_engine$ bundle exec rspec
Migrations are pending. To resolve this issue, run:

        bin/rails db:migrate RAILS_ENV=test

You have 1 pending migration:

20210215210420_create_sample_engine_widgets.rb
No examples found.
```

I have put the changes to see this in a branch here: https://github.com/jrmhaig/sample_engine/tree/second_model

I can switch back to the master branch, where there is only one model, and the `bundle exec rails db:drop db:create db:migrate RAILS_ENV=test; bundle exec rspec` works again as expected.

I'm sure there is something very simple I am missing but this has been driving me up the wall for the past day, and every single guide I have found online stops after creating a single model. Can anyone help?

Regards,

Joe

Phil Pirozhkov

unread,
Feb 15, 2021, 5:23:00 PMFeb 15
to Jack Royal-Gordon
Sorry, but probably it's a private repo, 404 for me.
> --
> You received this message because you are subscribed to the Google Groups "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/40cc291a-a7bc-456b-9299-21a156592a4an%40googlegroups.com.

Joseph Haig

unread,
Feb 16, 2021, 3:31:27 AMFeb 16
to rs...@googlegroups.com
Thank you for pointing this out. I thought that GitHub repositories were created public by default, but clearly not. It should be accessible now.

Phil Pirozhkov

unread,
Feb 16, 2021, 11:25:15 AMFeb 16
to Jack Royal-Gordon
I see the following in the `spec/dummy/config/database.yml`:

```
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
```

So, I've generated a migration
`db/migrate/20210216160820_add_sample_engine_grommets.rb` and added a
model `app/models/sample_engine/grommet.rb`

After that, I've run migrations for development env:
```
== 20210215210420 CreateSampleEngineWidgets: migrating ========================
-- create_table(:sample_engine_widgets)
-> 0.0055s
== 20210215210420 CreateSampleEngineWidgets: migrated (0.0057s) ===============

== 20210216160820 AddSampleEngineGrommets: migrating ==========================
-- create_table(:sample_engine_grommets)
-> 0.0039s
== 20210216160820 AddSampleEngineGrommets: migrated (0.0040s) =================
```

DBs reside in
spec/dummy/db/development.sqlite3
spec/dummy/db/test.sqlite3

I was surely getting the same message as you:
```
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test

You have 1 pending migration:
20210215210420_create_sample_engine_widgets.rb
```

```
bundle exec rails db:schema:load RAILS_ENV=test
```

After that, `rspec` runs just fine:

```
$ rspec
Warning: the running version of Bundler (2.1.4) is older than the
version that created the lockfile (2.2.3). We suggest you to upgrade
to the version that created the lockfile by running `gem install
bundler:2.2.3`.
*

Pending: (Failures listed here are expected and do not affect your
suite's status)

1) SampleEngine::Widget add some examples to (or delete)
/Users/pirj/source/tmp/sample_engine/spec/models/sample_engine/widget_spec.rb
# Not yet implemented
# ./spec/models/sample_engine/widget_spec.rb:5


Finished in 0.00592 seconds (files took 2.1 seconds to load)
1 example, 0 failures, 1 pending
```

To double-check, I've added `spec/models/sample_engine/grommet_spec.rb`:

```
require 'rails_helper'

module SampleEngine
RSpec.describe Grommet, type: :model do
it do
Grommet.create(name: 'hello')
expect(Grommet.count).to eq(1)
end
end
end
```

and it passes just fine.
Well, only for the first time, because `use_transactional_fixtures` is off.

Hope all that helps.

- Phil

Joseph Haig

unread,
Feb 16, 2021, 12:46:02 PMFeb 16
to rs...@googlegroups.com
Thanks Phil. So the trick is to use db:schema:load instead of db:migrate. I'd still like to know why migrating fails but at least this has given me a way forward.

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages