DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

Posted on โ€ข Edited on

3 3

my RSpec note

๐Ÿญ Preparing data(let, before)

Factorybot




๐Ÿงช Testing

expect

block needed matcher

it{ expect{something.save}.to change{ SomethingRelation.count }.by(3) }
it{ expect{something.save!}.to raise_error(StandardError) }

Model Spec

Controller/Request Spec

Feature Spec

๐Ÿงช Advanced Testing

shared example/context



Stub / Dowble

before{ allow_any_instance_of(ClassName).to receive(:a_method).and_return('great!') }
before{ allow_any_instance_of(ClassName).to receive(:a_method).and_raise(StandardError.new("error"))  }
before{ ClassName.any_instance.stub(:a_method).and_return('cool!') }
before{ ClassName.any_instance.stub(:a_method).and_raise('error!') }

๐Ÿ˜Ž Run RSpec

bundle exec rspec
bundel exec rspec spec/models/xxxx_spec.rb           ## specify specific file
bundel exec rspec spec/models/xxxx_spec.rb -e status ## specify a test in specific file

Troubleshooting

Sometimes, the test DB is not migrated(updated). If it occurs after you run rake db:rollback, then try this.

rake db:rollback RAILS_ENV=test
rake db:migrate RAILS_ENV=test

Image of Docusign

๐Ÿ› ๏ธ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Image of Timescale

Timescale โ€“ the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free