代码之家  ›  专栏  ›  技术社区  ›  Godzilla74

使用RSpec设计用户测试

  •  1
  • Godzilla74  · 技术社区  · 9 年前

    我已经将Devise与我的RoR应用程序集成,现在正在尝试测试我的控制器,特别是将我路由到 root_url .

    我已经在Device的页面上使用了这个HOWTO来设置我的管理员/用户工厂,但还有一个附加组件是我的用户注册过程的一部分,它正在创建一个 Company .

    因此:

    User : has_one :company

    公司 : has_many :users

    新用户的流程如下所示:

    1. 用户注册
    2. 用户确认帐户(通过电子邮件)并重定向到登录页面
    3. 用户登录
    4. 用户填写 公司 信息和提交
    5. 然后将用户重定向到 Pages#home (哪个是我的 root_url )

    使用Devise的HOWTO,我创建了一个 ControllerHelpers 文件内 Support :

    module ControllerHelpers
      def login_user
        before(:each) do
          @request.env["devise.mapping"] = Devise.mappings[:user]
          user = FactoryGirl.create(:user)
          user.confirm # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
          sign_in user
        end
      end
    end
    

    我怀疑我的 User Factory 因为它看起来不像 公司 正在创建,但我对RSpec太陌生了,我不确定。

    FactoryGirl.define do
      factory :user do
        first_name "Test"
        last_name "User"
        full_name "Test User"
        email "test@user.com"
        phone_number "111-222-3333"
        terms_accepted true
        time_zone "Central Time (US & Canada)"
        password "password"
        password_confirmation "password"
        confirmed_at Date.today
        association :company
      end
    end
    

    我有一个 company.rb 工厂也:

    FactoryGirl.define do
    
      factory :company do
        id 1
        name "ACME Test"
        address_1 "123 Shady Lane."
        address_2 "Suite 400"
        city "Testville"
        state "Test"
        zip_code "12345"
        has_payment_plan false
        stripe_id "cus_34d434343e4e3e3"
        locked false
      end
    end
    

    我的 pages_controller_spec.rb 在这一点上很简单:

    需要“rails_helper”

    RSpec.describe PagesController, :type => :controller do
    
      describe "User: GET #home" do
        login_user
    
        it "signs in the user" do
          expect(response).to render_template(:home)
        end
      end
    end
    

    这会导致以下RSpec错误:

    1) PagesController User: GET #home signs in the user
     Failure/Error: expect(response).to render_template(:home)
       expecting <"home"> but was a redirect to <http://test.host/companies/new>
     # ./spec/controllers/pages_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
    

    所以,它甚至没有做 render_template 我测试的一部分?

    更新:添加家庭控制器

    controllers/pages_controller#home

    def home
        if current_user && current_user.company
          verify_subscription
          get_company_and_locations
          get_network_hosts
          get_network_hosts_at_risk
          @network_hosts_snip = @network_hosts_at_risk.sort_by{ |h| -h.security_percentage }.first(5)
          get_company_issues
          @issues = @issues.sort_by{ |i| -i.cvss_score }.first(5)
          @deferred_issues = @company.deferred_issues.last(5)
          @deferred_hosts = @company.deferred_hosts.last(5)
        else
          redirect_to new_company_path
        end
      end
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   VAD    9 年前

    我们在聊天中发现…你的 company 已创建,但未持久化到数据库。因为你有 strategy: :build 在你的工厂里。

    策略::构建 意味着将创建关联对象,但不会持久化到数据库。要保持它,您应该使用 strategy: :create .或在您的用例中,您可以替换 association :company, strategy: :build 只是 公司 .FactoryGirl足够聪明,可以将其识别为必须创建和保持的关联。

    你需要建立FactoryGirl之间的关联 公司 subscription 同样的方式。

    推荐文章