Automatically generate missing unit tests for Rails models.
Did you forget to write test for your Rails models? Oughta can help, by automatically generating a lot of the test files and assertions for you.
Using the collection of matchers from Thoughtbot's Shoulda, oughta will parse your model code and oughta-matically generate assertions for your existing code.
Suppose you have a model like this:
classUser < ApplicationRecord# ==============================================================================# = Associations =# ==============================================================================has_many:comments,dependent: :delete_allbelongs_to:company,required: true# ==============================================================================# = Validations =# ==============================================================================validates:name,presence: {on: :update}validates:username,uniqueness: {scope: 'company_id'},presence: truevalidates:age,numericality: {allow_nil: true,greater_than: 17}validates:email,length: {in: 15..50}endOughta will automatically generate a spec file that looks like this:
require"rails_helper"RSpec.describeUser,type: :modeldodescribe"validations"doit{shouldvalidate_presence_of(:company).with_message(:required)}it{shouldvalidate_presence_of(:name).on(:update)}it{shouldvalidate_uniqueness_of(:username).scoped_to(:company_id)}it{shouldvalidate_presence_of(:username)}it{shouldvalidate_numericality_of(:age).allow_nil.is_greater_than(17)}it{shouldvalidate_length_of(:email).is_at_least(15).is_at_most(50)}enddescribe"associations"doit{shouldhave_many(:comments).dependent(:delete_all)}it{shouldbelong_to(:company).required}endendAdd this line to your application's Gemfile:
group:developmentdogem'oughta'endAnd then execute:
$ bundle install
Or install it yourself as:
$ gem install oughta
To generate specs for a model, use the following command:
$ rails g oughta:model_spec [YourModel]
To request a new feature or a change to an existing feature, please start a discussion here: https://github.com/Bodacious/oughta/discussions/
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/bodacious/oughta.
Let's make the world a better place, by reducing the amount of untested code.
If Oughta has saved you some time or made your life easier, please consider supporting future development with a small contribution to Buy Me a Coffee ☕.
The gem is available as open source under the terms of the MIT License.