RSpec 新手:Devise/Cancan 导致其他正常工作的控制器规范失败
我正在尝试让 RSpec 控制器规范通过。它与脚手架生成的规范几乎相同,只是用户首先登录到设备。 如果我从控制器禁用“load_and_authorize_resource”(检查权限),一切正常。但如果我把该行放回去,它就会失败:
1) PostsController logged in administrator POST create with valid params assigns a newly created post as @post
Failure/Error: post :create, :post => {'title' => 'test title'}
<Post(id: integer, title: string, cached_slug: string, content: text, user_id: integer, created_at: datetime, updated_at: datetime) (class)> received :new with unexpected arguments
expected: ({"title"=>"test title"})
got: (no args)
# ./spec/controllers/posts_controller_spec.rb:52:in `block (5 levels) in <top (required)>'
我假设规范没有正确登录用户,但 put current_user.role.name 确认用户已正确登录,并且具有必要的角色。在浏览器中执行实际过程确认其按预期工作。
有人有什么建议吗?我很困惑。下面的控制器:
def create
@post = Post.new(params[:post])
@post.user = current_user
respond_to do |format|
if @post.save
flash[:notice] = "Post successfully created"
format.html { redirect_to(@post)}
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
...以及规范
describe "with valid params" do
it "assigns a newly created post as @post" do
Post.stub(:new).with({'title' => 'test title'}) { mock_post(:save => true) }
post :create, :post => {'title' => 'test title'}
assigns(:post).should be(mock_post)
end
...以及规范中的支持内容:
before(:each) do
@user = Factory(:admin)
sign_in @user
end
def mock_post(stubs={})
@mock_post ||= mock_model(Post, stubs).as_null_object
end
非常感谢...
I'm trying to get an RSpec controller spec to pass. It's almost identical to the scaffold-generated spec, except a user is signed into devise first. If I disable 'load_and_authorize_resource' from the controller (which checks permissions), everything works fine. But if I put the line back in, it fails with:
1) PostsController logged in administrator POST create with valid params assigns a newly created post as @post
Failure/Error: post :create, :post => {'title' => 'test title'}
<Post(id: integer, title: string, cached_slug: string, content: text, user_id: integer, created_at: datetime, updated_at: datetime) (class)> received :new with unexpected arguments
expected: ({"title"=>"test title"})
got: (no args)
# ./spec/controllers/posts_controller_spec.rb:52:in `block (5 levels) in <top (required)>'
I had assumed the spec wasn't logging in the user correctly, but a puts current_user.role.name confirms the user is logged in correctly, and has the necessary role. Performing the actual process in a browser confirms it works as desired.
Anyone have any suggestions? I'm quite stumped. Controller below:
def create
@post = Post.new(params[:post])
@post.user = current_user
respond_to do |format|
if @post.save
flash[:notice] = "Post successfully created"
format.html { redirect_to(@post)}
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
...And the spec
describe "with valid params" do
it "assigns a newly created post as @post" do
Post.stub(:new).with({'title' => 'test title'}) { mock_post(:save => true) }
post :create, :post => {'title' => 'test title'}
assigns(:post).should be(mock_post)
end
...And supporting stuff in the spec:
before(:each) do
@user = Factory(:admin)
sign_in @user
end
def mock_post(stubs={})
@mock_post ||= mock_model(Post, stubs).as_null_object
end
Many thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 CanCan 升级到版本 1.5。我早些时候遇到过这个问题,但我认为升级后它就消失了。
Try upgrading CanCan to version 1.5. I had the issue earlier but I think it went away when I upgraded.