Question

I'm following the steps in the book Multitenancy with Rails by Ryan Bigg I came across with this error and I can not find what I'm doing wrong.

Error:

  1) Accounts Creating an account
     Failure/Error: page.should have_content('Signed in as subscribem@example.com')
       expected there to be text "Signed in as subscribem@example.com" in "Your account has been successfully created. Account Sign Up"
     # ./spec/features/accounts/sign_up_spec.rb:13:in `block (2 levels) in <top (required)>'

sign_up_spec.rb

require 'spec_helper'
feature 'Accounts' do 
  scenario "Creating an account" do
    visit subscribem.root_url
    click_link 'Account Sign Up'
    fill_in 'Name', :with => "Test"
    fill_in 'Email', :with => 'subscribem@example.com'
    password_field_id = 'account_owner_attributes_password'
    fill_in 'Password confirmation', :with => 'password'
    click_button 'Create Account'
    success_message = 'Your account has been successfully created'
    page.should have_content(success_message)
    page.should have_content('Signed in as subscribem@example.com')
  end
end                                                                                         

Account Model

module Subscribem
  class Account < ActiveRecord::Base
    attr_accessible :name, :owner_attributes

    belongs_to :owner, :class_name => 'Subscribem::User'
    accepts_nested_attributes_for :owner
  end
end

User Model

module Subscribem
  class User < ActiveRecord::Base
    attr_accessible :email, :password, :password_confirmation
    has_secure_password

  end
end

Application Controller

module Subscribem
  class ApplicationController < ActionController::Base

    def current_user
      if user_signed_in?
        Subscribem::Account.find(env['warden'].user(:scope => :account))
      end
    end

    helper_method :current_account

    def current_user
      if user_signed_in?
        Subscribem::User.find(env['warden'].user(:scope => :user))
      end
    end

    helper_method :current_user

    def user_signed_in?
      env['warden'].authenticated?(:user)
    end

    helper_method :user_signed_in?

  end
end  

Accounts controller

require_dependency "subscribem/application_controller"

module Subscribem
  class AccountsController < ApplicationController

    def new
      @account = Subscribem::Account.new
      @account.build_owner
    end

    def create
      account = Account.create(params[:account])
      env['warden'].set_user(account.owner.id, :scope => :user)
      env['warden'].set_user( account.id, :scope => :account)
      flash[:success] = "Your account has been successfully created."
      redirect_to subscribem.root_url
    end

  end
end

application.html.erb

<body>                                                                             
<% flash.each do |k,v| %>
<div class='flash <%= k %> '><%= v %></div>
<% end %>

<% if current_user %>
Signed in as <%= current_user.email %>
<% end %>

<%= yield %>

</body>
Was it helpful?

Solution

I had the same problem and solved it with a different expectation:

require 'rails_helper'

feature 'Accounts' do
  scenario 'creating an account' do
    visit subscribem.root_path
    click_link 'Account Sign Up'
    fill_in 'Name', with: 'Test'
    click_button 'Create Account'
    success_message = 'Your Account has been successfully created.'
    expect(find('.flash.success')).to have_content(success_message)
  end
end

The solution is to find the flash message for the success message instead of the page. Hope this helps others.

OTHER TIPS

Maybe the error is in view (subcribem/accounts/show.html.[template_engine])? It seems only the flash message is printed out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top