• Stars
    star
    216
  • Rank 183,179 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rails upgrade's best friend

still_life

What's This?

still_life is a testing framework enhancements for test-unit, minitest, RSpec, and Capybara that records all HTML response body texts that are rendered during E2E or unit test executions.

So What?

You can compare actually rendered HTML results before and after any app updates.

For What?

By comparing all these HTML files that are processed before and after any kind of code change, you can make sure that you did not (or you did) introduce any new user-facing incompatibilities. This may greatly help you for example, refactoring your app, replacing external libraries, or upgrading libraries. My personal use case that made me gemifying still_life was that I wanted to make sure that my own template engine renders the same HTML as the one that I was using.

But indeed, the real sweet spot of this tiny library is IMO "Rails upgrade". In fact, The first original version of this tool was implemented as an RSpec monkeypatch while we were upgrading a huge Rails application from Rails 2 to Rails 3.

Installation

Bundle still_life gem to your Rails app's :test environment.

gem 'still_life', group: :test

Usage

Run tests with an envvar STILL_LIFE. Then still_life creates some HTML files under tmp/html/#{ENV['STILL_LIFE']}/ directory. Each .html file is named from the location in your test code where the request was made.

For instance, if you run the tests against a simple scaffold app, the generated files will be like this:

% STILL_LIFE=rails52 rails test:system test
% tree tmp/html
tmp/html
โ””โ”€โ”€ rails52
    โ””โ”€โ”€ test
        โ”œโ”€โ”€ controllers
        โ”‚ย ย  โ”œโ”€โ”€ users_controller_test.rb-14.html
        โ”‚ย ย  โ”œโ”€โ”€ users_controller_test.rb-20.html
        โ”‚ย ย  โ”œโ”€โ”€ users_controller_test.rb-27.html
        โ”‚ย ย  โ”œโ”€โ”€ users_controller_test.rb-32.html
        โ”‚ย ย  โ”œโ”€โ”€ users_controller_test.rb-37.html
        โ”‚ย ย  โ”œโ”€โ”€ users_controller_test.rb-43.html
        โ”‚ย ย  โ””โ”€โ”€ users_controller_test.rb-9.html
        โ””โ”€โ”€ system
            โ”œโ”€โ”€ users_test.rb-14.html
            โ”œโ”€โ”€ users_test.rb-18.html
            โ”œโ”€โ”€ users_test.rb-21.html
            โ”œโ”€โ”€ users_test.rb-25.html
            โ”œโ”€โ”€ users_test.rb-26.html
            โ”œโ”€โ”€ users_test.rb-29.html
            โ”œโ”€โ”€ users_test.rb-32.html
            โ”œโ”€โ”€ users_test.rb-36.html
            โ”œโ”€โ”€ users_test.rb-37.html
            โ””โ”€โ”€ users_test.rb-9.html

4 directories, 17 files

And each file content is just an HTML.

% cat tmp/html/rails52/test/system/users_test.rb-18.html
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
    <title>StillLifeTest</title>
    
    

    <link rel="stylesheet" media="all" href="/assets/application-35729bfbaf9967f119234595ed222f7ab14859f304ab0acc5451afb387f637fa.css" data-turbolinks-track="reload" />
    <script src="/assets/application-3c2e77f06bf9a01c87fc8ca44294f3d3879d89483d83b66a13a89fc07412dd59.js" data-turbolinks-track="reload"></script>
  </head>

  <body>
    <p id="notice">User was successfully created.</p>

<p>
  <strong>Name:</strong>
  MyString
</p>

<a href="/users/980190963/edit">Edit</a> |
<a href="/users">Back</a>

  

</body></html>

Usage Scenario

Consider you have a well-tested Rails 5.2 app, and you want to upgrade its Rails version to 6.0 without introducing any user-facing incompatibilities. Then the workflow will be as follows:

1. Draw a still_life with the 5.2 app

% STILL_LIFE=rails52 rails test:system test

2. Do the upgrade job

% bundle u
% rails app:update

and push some more commits...

3. Draw another still_life with the 6.0 app

% STILL_LIFE=rails60 rails test:system test

4. Compare the results, and make sure there's no unexpected diffs

% git diff --no-index --color-words tmp/html/rails52/ tmp/html/rails60/

Notes

git diff

As written in the above example, git diff --no-index --color-words should perfectly work for recursively comparing two output directories.

Random Values

If your response includes some kind of random values, the test results may change between each test runs. In such case, maybe you could specify a random seed, or mock the random source in your app.

TODOs / Known Issues

  • The Capybara monkeypatch sometimes fails to get the page.body due to Capybara timing problem
  • Support older versions of Rails, Capybara, and Ruby
  • Fix the CI with RSpec + headless Chrome

Contributing

Pull requests are welcome on GitHub at https://github.com/amatsuda/still_life.

Credit

The original idea of this library was implemented as a 10 LOC anonymous module by @hotchpotch at Cookpad Inc. back in 2011 as written in this slide.

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

jb

A simple and fast JSON API template engine for Ruby on Rails
Ruby
1,235
star
2

active_decorator

ORM agnostic truly Object-Oriented view helper for Rails 4, 5, 6, and 7
Ruby
1,051
star
3

traceroute

A Rake task gem that helps you find the unused routes and controller actions for your Rails 3+ app
Ruby
877
star
4

heavens_door

Capybara test scenario recorder for Rails
JavaScript
863
star
5

database_rewinder

minimalist's tiny and ultra-fast database cleaner
Ruby
807
star
6

stateful_enum

A very simple state machine plugin built on top of ActiveRecord::Enum
Ruby
606
star
7

kaminari_themes

HTML
354
star
8

erd

A Rails engine for drawing your app's ER diagram
Ruby
332
star
9

html5_validators

A gem/plugin for Rails 3, Rails 4, Rails 5, and Rails 6 that enables client-side validation using ActiveModel + HTML5 Form Validation
Ruby
304
star
10

i18n_generators

A pack of Rails generators gem plugin that generates Rails 3 and Rails 2 I18n locale files for almost every known locale.
Ruby
285
star
11

himl

HTML-based Indented Markup Language for Ruby
Ruby
235
star
12

gem-src

Gem.post_install { `git clone gem_source src` }
Ruby
208
star
13

motorhead

A Rails Engine framework that helps safe and rapid feature prototyping
Ruby
181
star
14

nested_scaffold

Nested scaffold generator for Rails 4.2 and 5
Ruby
176
star
15

roundabout

A Rails Engine that generates a page transition diagram for your Rails app from request specs
Ruby
153
star
16

rfd

Ruby on Files & Directories
Ruby
152
star
17

routes_lazy_routes

A boot time booster for Ruby on Rails that defers loading the whole bloody routes so the app can spin up quickly ๐Ÿค˜
Ruby
141
star
18

string_template

A template engine for Rails, focusing on speed, using Ruby's String interpolation syntax
Ruby
125
star
19

kawaii_validation

An ActiveRecord extension that adds more kawaii validation syntax
Ruby
117
star
20

interactive_rspec

RSpec on IRB
Ruby
86
star
21

hocus_pocus

A magical isolated engine gem for Rails 3.1+
Ruby
80
star
22

ljax_rails

render :partial lazy-loader for Rails
Ruby
66
star
23

everywhere

Hash condition syntax for AR query everywhere!
Ruby
58
star
24

kaminari_example

A tutorial project for the basic and advanced usage of Kaminari paginator
Ruby
45
star
25

async_partial

Ruby
33
star
26

turbo_partial

Ruby
27
star
27

future_records

Ruby
25
star
28

lightweight_attributes

Ruby
24
star
29

more_optimized_resolver

Ruby
23
star
30

turbo_urls

Ruby
22
star
31

teriyaki

Automatically imports *_path definitions from config/routes.rb for acceptance testing
Ruby
22
star
32

kawaii_association

An ActiveRecord DSL extension that provides kawaii association syntax
Ruby
21
star
33

arel_ruby

ARel Ruby visitor
Ruby
20
star
34

activerecord-refinements

ActiveRecord + Ruby 2.0 refinements
Ruby
20
star
35

polymorphic_url_cache

Ruby
17
star
36

gem_i

A RubyGems plugin that explicitly aliases `gem i` to `gem install` to avoid ambiguity
Ruby
17
star
37

speed_king

Ruby
14
star
38

nested_layouts

The only fork of "nested_layouts" Rails plugin in Github that correctly bug fixed for Rails 2.3
Ruby
13
star
39

rspec-refinements

RSpec + Ruby 2.0 refinements
Ruby
10
star
40

arenai

Ruby
8
star
41

activecalendar

Rails 2.2.2 ready javascript calendar date renderer
JavaScript
8
star
42

bundler-squash

Ruby
8
star
43

rbenv-gem-shared

Ruby
8
star
44

tatsuzine

Live coded app at Railsๅ‹‰ๅผทไผš@ๆฑไบฌ#59
Ruby
8
star
45

bot_for_ruby-lang

Ruby
7
star
46

factory_factory

a script that transfers existing AR models into factories for factory_girl
Shell
6
star
47

automagic

Ruby
6
star
48

snowman_meltdown

A simple middleware for Rails 3 to vanish _snowman parameterโ˜ƒโ˜ƒโ˜ƒ
Ruby
5
star
49

activesupport-refinements

Ruby
4
star
50

webdb073_tutorial

WEB+DB Press Vol. 73 ็‰น้›†2ใ€Œ่ฉณ่งฃRails 4ใ€ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใฎใ‚ตใƒณใƒ—ใƒซใ‚ณใƒผใƒ‰
Ruby
4
star
51

gitrockets

Ruby
3
star
52

rails3_hands_on

ๆฑไบฌRubyไผš่ญฐ03ใฎใƒฏใƒผใ‚ฏใ‚ทใƒงใƒƒใƒ—ใ€ŒRails 3ใƒใƒณใ‚บใ‚ชใƒณใ€ใฎใ‚ตใƒณใƒ—ใƒซใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณ
Ruby
3
star
53

prsnt

prsnt prttyp
3
star
54

git_commands

3
star
55

internationalization

Ruby
3
star
56

qwik

qwik
2
star
57

hfrails

hfrails
2
star
58

atode_yomu

A gem plugin that cleverly installs rdoc and ri for the latest versions of already installed gems
Ruby
2
star
59

gem-diet

Ruby
2
star
60

action_args_with_rbs

Ruby
1
star