Rspec test for passing validation in post (sample app)

Jan 26, 2018 by ohiodn8




rails new malboro

IN GEMFILE ==>>

  gem "rspec-rails"    
  gem "guard-rspec"

CMD ==>>

bundle install
rails g scaffold post title:string body:text
rails g rspec:install

To generate an Integration Test ==>>

rails g integration_test post

## MIGRATE DATABASE => rake db:migrate db:test:prepare
TO TEST (TRY IT OUT), RUN==>>

rake spec:requests

TIME TO INTEGRATE GUARD FOR CONTINUOUS TESTING  ==>>

guard init

TO START GUARD ==>>

guard


## * ADD MORE SCRIPT TO GENERATED INTEGRATION POST SPEC in SPEC/REQUESTS/POSTS_SPEC.RB   (NOT NOW)  *

MODEL RSPEC FOR TDD //spec/models/post_spec.rb

RSpec.describe Post, type: :model do
  context 'validation tests' do
    it 'ensures title presence' do
      post = Post.new(title: 'Last title first').save
      expect(post).to eq(false)
    end
   
    it 'ensures body presence' do
      post = Post.new(body: 'Last title first musicals').save
      expect(post).to eq(false)
    end
   
    it 'should save successfully' do
      post = Post.new(title: 'Last title first', body: 'Last title first musicals').save
      expect(post).to eq(true)
    end
  end

  context 'scope tests' do
  end
end

## GUARD SHOULD RETURN SOME FAILURE
SET YOUR VALIDATION IN POST MODEL ==>>

    validates :title, presence: true 
    validates :body, presence: true

### GUARD TEST SHOULD PASS
## IN ROOT DIRECTORY .rspec, ADD => (gives detailed report)  ==>>

--format documentation

FOR spec/controllers/post_controller_spec.rb ==>>

  context 'GET #show' do
    it "returns a success response" do
      post = Post.create!(title: 'Last title first', body: 'Last title first musicals')
      get :show, params: {id: post.to_param}, session: valid_session
      expect(response).to be_success
    end
  end

  describe "GET #edit" do
    it "returns a success response" do
      post = Post.create!(title: 'Last title first', body: 'Last title first musicals')
      get :edit, params: {id: post.to_param}, session: valid_session
      expect(response).to be_success
    end
  end

Open another terminal/CMD and run => to run all your tests ==>>

rspec



Expect more improved & advanced testing in the future - - -



About ohiodn8

Ruby on Rails developer, AWS Engineer, anything fun, music, a little bit of mobile game. . .

Comments (1)

I mixed two of my documentation in this 1 tutorial. Pls ignore documentation till it is updated...

over 5 years ago by ohiodn8