سؤال

I am having the configuration as below :

Rakefile

require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = 'spec/*_spec.rb'
end

Gemfile

source 'https://rubygems.org'

gem 'sinatra'
gem 'thin'
gem 'rack-test'

spec_helper.rb

ENV['RACK_ENV'] = 'test'

require 'minitest/autorun'
require 'rack/test'
require_relative '../app'

include Rack::Test::Methods

def app
  MyApp
end

app_spec.rb

require_relative 'spec_helper'

describe 'Hello World' do

  it 'should have hello world' do
    get '/'
    last_response.must_be :ok?
    # I want to do something like below
    last_response.title must_match /home page/i
  end
end

How can I test the page title of a view using MiniTest and Sinatra.

هل كانت مفيدة؟

المحلول

You can't check this with minitest alone.

You should be looking at Capybara to achieve this.

Capybara helps you test web applications by simulating how a real user would interact with your app.

Once setup use has_title? matcher to test the title of the rendered page.

Using Capybara you can test various aspects of your page like its content/text, if it has a particular link or not, a particular button, text field, email field, etc. You can simulate the behavior of filling up a form and clicking on a button to submit the same. Putting it simply, it just behaves as a real user would by simulating the actions that a user can perform on a given page with what all a user can see on a given page.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top