我正在编写测试来使用RSpec测试我的控制器。如果用户登录或未登录,我想测试用户进入“新”页面时的情况。但测试失败了,我无法弄清楚原因。
这是我的测试:
describe "on getting /new" do
context "if signed in" do
before(:each) do
sign_in FactoryGirl.build(:user)
end
it "assigns a new Article to @article" do
get :new
expect(assigns(:article)).to be_a_new(Article)
end
it "renders the :new template" do
get :new
expect(response).to render_template :new
end
end
end
我的工厂:
FactoryGirl.define do
factory :user do
email "this@mail.com"
username "main_user"
password "password"
password_confirmation "password"
end
end
当我运行RSpec时,这就是我得到的:
Failures:
1) ArticlesController on getting /new if signed in assigns a new Article to @article
Failure/Error: expect(assigns(:article)).to be_a_new(Article)
expected nil to be a new Article(id: integer, title: string, text: string, created_at: datetime, updated_at: datetime, user_id: integer, shortname: string)
# ./spec/controllers/article_spec.rb:56:in `block (4 levels) in <top (required)>'
2) ArticlesController on getting /new if signed in renders the :new template
Failure/Error: expect(response).to render_template :new
expecting <"new"> but rendering with <[]>
# ./spec/controllers/article_spec.rb:61:in `block (4 levels) in <top (required)>'
如果未登录,我的应用应将用户重定向到根页面。
如果我在我的控制器中注释掉authenticate方法并注释掉sign_in
部分,那么测试将正常工作,所以我假设它是Devise问题。
我该如何解决?