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 と User には適切なポリモーフィックな関係がなく (これは別の話です)、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 に答える