def create
@url = encode_url(params[:url])
uri = URI(@url)
@host = Host.find_by_address(normalize_domain(uri.host))
@resource = @host.resources.new(path: get_full_path(uri), csspaths: params[:csspaths], period: params[:period])
if @resource.save
redirect_to '/settings/resources', notice: 'Resource was saved.'
else
render :new
end
end
Хочу для начала проверить просто @host.
let(:host) { mock_model(Host).as_null_object }
before do
Host.stub(:find).and_return(host)
end
it "should find @host" do
Host.should_receive(:find_by_address).and_return(host)
post :create, url: "opennet.ru"
end
Тест валится на попытке вызвать внешнюю функцию. Немного попытался написать для ней stub-ов, но нифига.
ResourcesController#create should find @host
Failure/Error: post :create, url: "opennet.ru"
NoMethodError:
undefined method `sub' for nil:NilClass
# ./app/controllers/application_controller.rb:22:in `normalize_domain'
# ./app/controllers/resources_controller.rb:17:in `create'
# ./spec/controllers/resources_controller_spec.rb:40:in `block (3 levels) in <top (required)>'