前にユーザーを作成するこんにちは、私はRSpecの(そして一般的にはユニットテスト)に新しいですし、次のメソッドをテストしたい:RSpecの:試験方法
class HelloController < ApplicationController
def hello_world
user = User.find(4)
@subscription = 10.00
render :text => "Done."
end
end
私はそうのようなRSpecのを使用しようとしています:
Describe HelloController, :type => :controller do
describe "get hello_world" do
it "should render the text 'done'" do
get :hello_world
expect(response.body).to include_text("Done.")
end
end
end
このメソッドが正しく動作し、テストが完了していることを単にテストしたいと思います。テストを実行すると、次のエラーが発生します。
Failure/Error: user = User.find(4)
ActiveRecord::RecordNotFound:
Couldn't find User with 'id'=4
しかし、実行する前にそのIDを持つユーザーを正しく作成するにはどうすればよいですか。私は他のチュートリアルや質問をもとに次のことを試してみましたが、それは動作しません:
describe "get hello_world" do
let(:user) {User.create(id: 4)}
it "should render the text 'done'" do
get :hello_world
expect(response.body).to include_text("Done.")
end
end
は、事前にありがとうございます。
これを確認してくださいhttps://github.com/thoughtbot/factory_girl_rails –