【问题标题】:Rspec / Authlogic / Capybara - incorrect redirectRspec / Authlogic / Capybara - 不正确的重定向
【发布时间】:2011-08-27 10:06:46
【问题描述】:

我是测试新手,我正在尝试在links_spec.rb 中编写测试。

describe "Links" do
before :each do
    activate_authlogic
    @company = Factory.build(:company)
    # next line creates errors
    @user = Factory.build(:user, :user_type => "Company", :user_type_id => @company.id)
    UserSession.create( @company.user )
end

it "redirects to index after create" do
    link = Factory.build(:link)
    post "links"
    response.should redirect_to(links_path)
end
end

首先,@user = Factory.build(...) 行抛出错误,因为它说用户重复 - 我不知道用户是在哪里创建的,因为公司和用户没有适当的多态关系(这是另一回事),并且 Company.rb 在创建时不会自动创建用户。

因此,当我删除该行时,一切似乎都很好,除非它到达 response.should redirect... 行。而不是重定向到links_path,测试失败并告诉我:

Failure/Error: response.should redirect_to(links_path)
   Expected response to be a redirect to <http://www.example.com/links> but was a redirect to <http://www.example.com/home>
 # ./spec/requests/links_spec.rb:13:in `block (2 levels) in <top (required)>'

我不明白为什么会发生这种情况或如何解决它。

【问题讨论】:

  • 可能有点晚了,但你能发布你的工厂吗?

标签: ruby-on-rails ruby-on-rails-3 authlogic capybara rspec-rails


【解决方案1】:

您是否在每次测试后使用database_cleaner 之类的东西来清空数据库?

# spec_helper.rb

config.before(:each) do
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

【讨论】:

    猜你喜欢
    • 2015-05-05
    • 1970-01-01
    • 2012-06-12
    • 2014-12-10
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多