1

我正在按照Ryan Bigg的《 Multitenancy with Rails 》一书中的步骤进行操作,我遇到了这个错误,但我找不到我做错了什么。

错误:

  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                                                                                         

账户模型

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

用户模型

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

  end
end

应用控制器

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  

帐户控制器

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

应用程序.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>
4

2 回答 2

0

我遇到了同样的问题并以不同的期望解决了它:

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

解决方案是找到成功消息的闪存消息而不是页面。希望这对其他人有帮助。

于 2014-08-26T08:54:03.277 回答
0

也许错误在视图中(subcribem/accounts/show.html.[template_engine])?似乎只打印了闪存消息。

于 2013-02-17T23:17:17.067 回答