我发现关于这个主题的帖子不够,所以我决定再次发布问题。
我只是想测试使用rspec设计的控制器。
我确实添加了rails_helper.rb
config.include Devise::TestHelpers, :type => :controller
我写了这个测试:
describe AlbumsController do
render_views
let(:user) { User.create!(email: "rspec@example.com", password: "password") }
before(:each) do
sign_in(user)
end
it "should have a current_user" do
subject.current_user.should_not be_nil
end
describe "get ALBUM index" do
describe "for a registred user" do
it "should => index page" do
get :index
response.should have_selector('div#photos')
end
end
end
end
我的索引方法真的执行了!我添加了一些看跌后续执行。 subject.current_user不为null。一切都应该是好的。
但我收到一条消息:
<html><body>You are being <a href=\"http://test.host/main\">redirected</a>.</body></html>
我的索引操作中没有特定的redirect_to或render方法。它只是呈现默认值。
这里有什么问题?
我使用的是宝石&#39;设计&#39;,&#34; 2.2.8&#34;和gem&#39; rails&#39;,&#34; 3.2.1&#34;。我知道它已经老了。我尝试在迁移到Rails 4之前添加测试: - /
编辑1:
current_user在我的索引方法中有效。 这意味着它不是一个设计问题。但是呈现的视图
答案 0 :(得分:1)
尝试使用此类构造
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
sign_in user
end