[英]Testing controller new action in Rspec
I'm trying to test my controller's new actin with Rspec but getting the following error: 我正在尝试使用Rspec测试我的控制器的新肌动蛋白,但收到以下错误:
Failure/Error: expect(assigns(:category)).to be_a_new(Category)
expected nil to be a new Category(id: integer, title: string, image_url: string, description: string, created_at: datetime, updated_at: datetime)
Here is the code for the test: 这是测试的代码:
describe "GET #new" do
it "assigns a new category as @category" do
get :new, params: { }, session: valid_session
expect(assigns(:category)).to be_a_new(Category)
end
end
Categories controller new action: 类别控制器新动作:
def new
@category = Category.new
end
Using get with the new action opens up a form where a new category can be created. 使用get with new action打开一个表单,可以在其中创建新类别。
If anyone could point me in the right direction that would be great. 如果有人能指出我正确的方向,这将是伟大的。
Thanks. 谢谢。
Try something like this: 尝试这样的事情:
describe "GET #new" do
before(:each) do
session[:user_id] = valid_session.id #or something like
get :new
end
it "should be success" do
expect(assigns(:category)).to_not eq nil
expect(assigns(:category)).to be_a_new(Category)
end
end
This way you set the session before get :new. 这样你就可以在get:new之前设置会话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.