0

I am trying to test a ruby authentication app using minitest and webrat but get errors.

Tests like visit '/' fail with an error Status 200 expected but was 404.

Tests containing code like fill_in :email, :with => "first@company.com" fail with error Could not find field: :email.

I read several sinatra, testing and webrat documents and forums. Some of them were old and suggested stuff like Sinatra::Default, but github.com/brynary/webrat/wiki/sinatra, Building a Sinatra App Driven By Webrat Tests and Learning From the Masters: Sinatra Internals are new, yet they still fail.

Basically, I didn't like sentence-like syntax of rspec, cucumber etc but do want to do behaviour driven development. I really like the minitest syntax, both tests and output and that is why I choose webrat for BDD. If I'm wrong about expecting webrat to fulfill acceptance testing requirements, please simply tell me that I should use this framework or that one.

Apart from that, the first parts of the main file and test file are below. I hope someone can explain me, what I am missing?

test_file

require "test/unit"
require "minitest/autorun"
require "rack/test"
require 'webrat'
require_relative "../lib/kimsin.rb"

Webrat.configure do |config|
  config.mode = :rack
end

ENV["RACK_ENV"] = "test"

class KimsinTests < Test::Unit::TestCase
  include Rack::Test::Methods
  include Webrat::Methods
  include Webrat::Matchers

  def app
    Sinatra::Application.new
  end

  def test_create_user
    visit "/user/new"
    fill_in :username, :with => "first@company.com"
    fill_in :password, :with => "abC123?*"
    fill_in :confirm_password, :with => "abC123?*"
    click_link "Register"
    assert 201, last_response.status, "Status 201 expected but was #{last_response.status}.\n#{error}"
    assert_contain /Logged in as first@company.com./, "No user created"
    assert_contain /Logout/, "Logout link not present"
  end

main_file

require "sinatra"
require "erb"
require_relative "../lib/kimsin/version"
require_relative "../lib/kimsin/user"

class Kimsin < Sinatra::Application
  use Rack::Session::Pool, :expire_after => 2592000
  set :session_secret, BCrypt::Engine.generate_salt

  configure :development do  
    DataMapper.auto_migrate!  
  end

  get "/" do
    if session[:user_id]
      user = User.get session[:user_id]
      email = user.email
      erb :index, :locals => { :email => email }
    else
      email = nil
      erb :index, :locals => { :email => email }
    end      
  end
Community
  • 1
  • 1
barerd
  • 835
  • 3
  • 11
  • 31

1 Answers1

0

Using Sinatra with Webrat should work fine. I think that the errors that you are seeing are caused by the following method (around line 18 in your test file):

def app
  Sinatra::Application.new
end

This is setting up the Sinatra::Application base class to run your tests against when you really need to set up your own subclass Kimsin (because you are creating a modular style Sinatra app), i.e.

def app
  Kimsin.new
end

The 404 errors and missing fields are happening because Sinatra::Application doesn't define any of the routes you are testing.

You might also like to take a look at Capybara if you are looking for similar alternatives to Webrat.

Steve
  • 15,606
  • 3
  • 44
  • 39
  • Thank you, that definitely started the app, since visiting urls is now working. But I still get errors like `Could not find field: :email` and `Could not find link with text or title or id "Login"`. I checked it with Capybara, too and it has its own errors, I added them on the relevant question. Is there anymore info or file I should provide to get help about this? – barerd Jun 16 '12 at 21:40
  • I don't see anything wrong with the `fill_in` or `click_link` calls that you are making so it seems likely that something else went wrong and those elements really are not there. A quick way to debug these kinds of problems is to call `save_and_open_page` just before the failing line in your test so that you can see the Web page at the point of failure. Often this reveals an error. You need to install the `launchy` gem to make this work. – Steve Jun 17 '12 at 08:53
  • save_and_open_page worked great. They showed me that my session[:errors] caused a problem in login page, if no user was registered before. I couldn't notice that accidentally, because my tests were in a strict order (get index, register new user, register bad user/password, login user etc..) – barerd Jun 17 '12 at 19:04