Reputation: 1
I've spent hours on findin information on how to test rails 4 app with new MiniTest, seems like everyone use rspec. I'm trying to test all levels of application, and currently stuck on how to test persisted data, for example I've User
model which is belongs to Account
and I want to test registration and authentication and all post-auth things. Currently all my test aren't persistent, model level test create users with each test-run.
However I want some data to be persistent, for example few User-Accounts, or linked projects with account. I don't want to recreate them with each run. But I wasn't able to find any information regarding rails 4 and MiniTest.
So what should I use to persist data? fixtures or seed.rb? How to reset data during controller \integration tests?
Upvotes: 0
Views: 1388
Reputation: 820
Every time you run your tests they are run on a clean database. Like other people said, you should be testing your application's logic! You can use FactoryGirl to add persistence to your tests. You can define your own factories and create objects out of them in your tests.
For example, defining a factory is easy:
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Doe"
admin false
end
end
After that, in your test, you can call this factory:
# Returns a User instance that's not saved in database
user = FactoryGirl.build(:user)
# Returns a saved (in database) User instance
user = FactoryGirl.create(:user)
Also, you can provide your own values for User's attributes:
jane_doe = FactoryGirl.create(:user, first_name: "Jane", last_name: "Doe")
In your situation, if you want to create a factory for the Account class and add an association (the User) to the factory:
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Doe"
admin false
end
factory :account do
acc_number 123456789
full_name "John Doe"
association :user, factory: :user
end
end
You can read more about FactoryGirl here
Also, about cleaning out your database, you can check out the Database Cleaner gem. It's easy to configure and it works!
Hope that helps!
Upvotes: 1
Reputation: 8984
You should use fixtures to provide a default set of data for your tests. ActiveSupport::TestCase will wrap each test in a DB transaction, rolling back all changes between tests. This means that you don't have to do any work to reset the DB in your controller or integration tests, it does it for you automatically.
Upvotes: 3
Reputation: 3272
If I understand you correctly (and I'm not entirely sure that I do), you might find that factory_girl does what you want in terms of providing "persistent data" you can use for testing.
Upvotes: 0
Reputation: 9691
The test database is cleared after each test run, to ensure that you have a clean slate. In general, you should be testing logic. Testing whether data is persisted is really Rails' job, and that is already covered by its own test suite.
If you really want to check for persistence, you could so something like:
lambda { user.save }.must_change "User.count", +1
But really, what you should test is your own logic, not whether the database works or not.
Upvotes: 1