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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay