0

我是测试新手,我正在尝试在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)>'

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

4

1 回答 1

0

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

# spec_helper.rb

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

config.after(:each) do
  DatabaseCleaner.clean
end
于 2011-11-10T14:32:17.123 回答