RSpec을 사용하여 뷰를 테스트할 때의 스텁 용량은?
묘사
안녕하세요. 저는 rspec 테스트를 작성하고 있습니다. 테스트 캐릭터가'user'인 사용자는'Edit user'링크를 보지 못했기 때문에 다른 사용자를 편집할 수 없습니다. 그러나 정의되지 않은 nil:NilClass 방법인'authenticate'를 받았습니다.나는 이 비밀번호가 있다.
before(:each) do @user_b = FactoryGirl.create(:user, :email=>"[email protected]", :password=>"132132132", :password_confirmation=>"132132132", :id=>34534578687, :role=>"user") end describe "Edit link" do it "should see it if the page shows the profile of the current user" do render rendered.should have_content(@user.email) rendered.should have_content(@user.role) rendered.should have_selector("a",:href=>"users/#{@user.id}/edit",:value=>"Edit") end end
and this is in shared_example and then in context "user is with role 'user'" I have this:
@user = FactoryGirl.create(:user,:email=>"[email protected]",:password=>"132132132", :password_confirmation=>"132132132",:id=>34534578686,:role=>"user") ApplicationController.stub(:current_user).and_return(@user)
but I get this error:
Failure/Error: render
ActionView::Template::Error:
undefined method `authenticate' for nil:NilClass
I tried the things that are in here: https://github.com/ryanb/cancan/wiki/Testing-Abilities but nothing worked. Has someone faced the same problem?
Thanks in advance.
Update: Formatted the code and the description.
토론 #1
Can you post the entirety of a minimal reproduction for this? It's unclear how you are including the shared example. The pieces you posted aren't enough for me to see what you are trying to do. If you could strip this down to a base case and post that, I may be able to help.
토론 #2
Can you please read the comment on another issue and try stubbing the methods out?
https://github.com/ryanb/cancan/issues/598#issuecomment-5669563
토론 #셋
I have User model generated with devise and I tried:
view.should_receive(:current_user).and_return(@user) and view.should_receive(:current_ability).and_return(Ability.new(@user))
but I still get the error:
Failure/Error: render ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
토론 #4
Here is my code:
require 'spec_helper'
describe "users/show.html.erb" do
shared_examples_for "user who can manage his profile" do
it "should see his email" do
view.should_receive(:current_abillity).and_return(Ability.new(@user))
view.should_receive(:current_user).and_return(@user)
render
rendered.should have_content(@user.email)
end
end
context "user with role 'user'" do
before(:each) do
@user = FactoryGirl.create(:user,:email=>"[email protected]",:password=>"132132132",
:password_confirmation=>"132132132",:id=>34534578687,:role=>"user")
end
it_behaves_like "user who can manage his profile"
end
Should I require some gems?
토론 #5
I tried this and it worked!:
require 'spec_helper'
describe "mymodel/index.html.erb" do
before(:each) do
assign(:my_model,mock_model(MyModel))
@ability = Object.new
@ability.extend(CanCan::Ability)
controller.stub(:current_ability) { @ability }
end
context "authorized user" do
it "can see the table header" do
@ability.can :destroy, MyModel
render
rendered.should have_selector('th:contains("Options")')
end
end
context "unauthorized user" do
it "cannot see the table header" do
render
rendered.should_not have_selector('th:contains("Options")')
end
end
end
토론 #6
Another viable possibility if this is for a view spec:
context 'condition when can? should be true, such as an admin context' do
before do
# the key line
allow(view).to receive(:can?).and_return(true)
end
it 'the test' do
# blah blah
end
# etc
end
이 상하문에서 can?만 실행하면 true의 테스트만 되돌려주고ifcan?를 위해 false 한 그룹의 단독 테스트만 실행해야 한다(예를 들어 한 그룹의 테스트는 사용자가 정상적인 사용자이고 다른 그룹의 테스트는 사용자가 관리자라고 가정한다).토론 #7
Another viable possibility if this is for a view spec:
``` context '[condition when can? should be true]' do before { allow(view).to_receive(:can?).and_return(true) } # this is the key line
it 'the test' do # blah blah end
# etc
end ```
Within that context, only run tests that
can?should returntrue, then run a separate set of tests for ifcan?returnsfalse(e.g. one set of tests as if the user is a normal user, another for if the user is an admin).
ffdlange에 작은 맞춤법 오류가 있습니다.
allow(view).to receive(:can?).and_return(true), to 와 receive 사이에 빈칸이 있을 것입니다.토론 #8
감사합니다 @domangi🙏, 나는 당신의 정정을 반영하기 위해 나의 회답을 업데이트했습니다Reference
이 문제에 관하여(RSpec을 사용하여 뷰를 테스트할 때의 스텁 용량은?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://github.com/ryanb/cancan/issues/602텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)