我将如何使用 Rspec 测试此控制器操作
我应该如何指定这个
class FlagsController
def like
flag = current_user.flags.find_or_initialize_by_flaggable_type_and_flaggable_id(params[:like_type], params[:like_id])
flag.kind = params[:kind]
if flag.save
head :ok
else
head :unprocessable_entity
end
end
end
我当前的规格
describe FlagsController do
before (:each) do
@user = Factory(:user)
sign_in @user
end
describe "flag" do
it 'with valid params' do
controller.stub!(:current_user).and_return(@user)
@user.stub_chain(:flags, :find_or_initialize_by_flaggable_type_and_flaggable_id).
with('Comment', '1').
and_return(mock_flag(:save => false)).
stub!(:kind).
with('up').
and_return(mock_flag(:save => true))
post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
response.response_code.should eq(200)
end
end
规格结果
Failure/Error: response.response_code.should eq(200)
expected 200
got 422
(compared using ==)
路线
post 'like' => 'flags#like'
How should I spec this
class FlagsController
def like
flag = current_user.flags.find_or_initialize_by_flaggable_type_and_flaggable_id(params[:like_type], params[:like_id])
flag.kind = params[:kind]
if flag.save
head :ok
else
head :unprocessable_entity
end
end
end
My current spec
describe FlagsController do
before (:each) do
@user = Factory(:user)
sign_in @user
end
describe "flag" do
it 'with valid params' do
controller.stub!(:current_user).and_return(@user)
@user.stub_chain(:flags, :find_or_initialize_by_flaggable_type_and_flaggable_id).
with('Comment', '1').
and_return(mock_flag(:save => false)).
stub!(:kind).
with('up').
and_return(mock_flag(:save => true))
post :like, :like_type => 'Comment', :like_id => '1', :kind => 'up'
response.response_code.should eq(200)
end
end
Spec results
Failure/Error: response.response_code.should eq(200)
expected 200
got 422
(compared using ==)
Route
post 'like' => 'flags#like'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将如何编写这个测试:
希望这会有所帮助。
How I would write this test:
Hope this helps.