【发布时间】:2016-08-25 20:36:08
【问题描述】:
您好,我是 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
提前谢谢你。
【问题讨论】:
标签: ruby-on-rails unit-testing rspec