Skip to content

Reproducing bugs using inline test

Nguyễn Đức Long edited this page Aug 22, 2018 · 2 revisions
# You can run this file directly using `ruby` command
# Given bundler and rspec 3 are installed globally
# It might be different if you are using rspec 2
require 'bundler/inline'
require 'rspec/autorun'
require 'rspec/core/formatters/progress_formatter'
require 'rspec/core/profiler'

# TODO: Define dependencies
gemfile true do
  source 'https://rubygems.org'
  gem 'sqlite3'
  gem 'rspec-expectations'

  # TODO: Remember to point to the correct version or Github commit reference
  gem 'activerecord', '~> 5.2'
  # gem 'activemodel'
  gem 'shoulda-matchers', '~> 3.0'
end

# TODO: Require the necessary files here if not required by bundler
require 'active_record'
require 'active_record/relation'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

# TODO: Define schema structure
ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
  end

  create_table :posts, force: true do |t|
    t.references :user, index: true, foreign_key: true
  end
end

# TODO: Define models
class User < ActiveRecord::Base
end

class Post < ActiveRecord::Base
  belongs_to :user
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec

    # Choose one or more libraries:
    with.library :active_record
  end
end

# TODO: Write your tests here
RSpec.describe Post, type: :model do
  # Should pass
  it { is_expected.to belong_to(:user) }

  # Deliberated failure
  it { is_expected.to belong_to(:moderator) }
end