RSpec2의 컨트롤러를 테스트하고 있으며 작성 및 업데이트 작업 모두에 대해 유효하지 않은 매개 변수가 전달되면 컨트롤러는 "new"또는 " 편집 "템플릿을 사용합니다. 그것은 그렇게하고 있지만, 내 테스트는 결코 통과하지 못합니다. 이에Rspec2 : 유효하지 않은 params가 실패한 후 response.should render_template ("new")
describe "with invalid params" do
before(:each) do
User.stub(:new) { mock_user(:valid? => false, :save => false) }
end
it "re-renders the 'new' template" do
post :create, :company_id => mock_company.id
response.should render_template("new")
end
end
결과 :
여기re-renders the 'new' template
expecting <"new"> but rendering with <"">
는 컨트롤러 액션이다는 :
respond_to do |format|
if @user.save
format.html {
flash[:notice] = "#{@user.full_name} was added to #{@company.name}."
redirect_to company_users_url(@company)
}
else
logger.debug @user.errors
format.html{
render :new
}
end
end
이 문제는이 컨트롤러에 고립 된 것으로 보인다. 다른 컨트롤러를 실행하는 거의 동일한 코드가 있는데 괜찮습니다. 문제가 어디 있는지 알 수 없습니다.
업데이트 : 정보가있다 여기에 은 두 모의 방법
def mock_user(stubs={})
@mock_user ||= mock_model(User, stubs).as_null_object
end
def mock_company(stubs={})
(@mock_company ||= mock_model(Company).as_null_object).tap do |company|
company.stub(stubs) unless stubs.empty?
end
end
'mock_user'와'mock_company'의 내용을 게시 할 수 있습니까? – zetetic
아직 뭔가가 누락되었습니다. 컨트롤러에서'@ company'와'@ user'의 값을 어떻게 설정합니까 (아마 before 필터를 사용합니까?). 컨트롤러가 실제로'User.new'를 호출합니까? – zetetic