Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running Rails unit tests without loading the environment or using a database

As something of an academic exercise, I thought I'd try to get my unit tests running without loading the Rails env (or hitting the database).

I've seen this done before, and it seems like folks talk about it enough, but I can't find any good/current resources on getting it done.

Does anyone have any good stories or a nice long blog post about how to do this?

like image 457
whatbird Avatar asked Aug 16 '11 14:08

whatbird


People also ask

Should unit test include database?

Database access falls outside the scope of unit testing, so you would not write unit tests that include database access. You would include database access testing in your functional tests. Similarly, if you have a network app, you would not include network access in your unit tests.

How do you run a test in rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

How do you run a Minitest in rails?

To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.

What is Minitest in Ruby on rails?

What is Minitest? Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.


1 Answers

An useful link: testing Rails without Rails

Testing without database would involve much mocks & stubs, nothing special to add.

like image 67
apneadiving Avatar answered Nov 15 '22 04:11

apneadiving