caelum / restfulie

CRUD through HTTP is a good step forward to using resources and becoming RESTful. Another step further into it is to make use of hypermedia-based services and the Restfulie gem allows you to do it very quickly.
Other
393 stars 43 forks source link

Restfulie not called with RSpec tests #70

Closed ghost closed 13 years ago

ghost commented 13 years ago

When the controller is executed normally, everything looks fine and I get a correct JSON/XML response. However, trying to do the same in RSPEC it looks like Restfulie isn't called at all. The controller action is called with the correct params and also gets the models, but the tokamak builder (view) isn't called.

CONTROLLER

def index
  puts "prams #{params}"
  puts "INDEX #{current_projects.size}"
  respond_with(current_projects)
end

SPEC

it "should return all projects" do
  get :index, :format => "json"
  puts "headers #{response.headers}"
  puts "RESPONSE CODE '#{response.response_code}'"
  response.response_code.should == 200
  puts "RESPONSE '#{response.body}'"
  ....

==> response code is 200, however, the body is empty. Headers are {"Content-Type"=>"application/json; charset=utf-8", "Cache-Control"=>"no-cache"}

I am pretty stuck with this. One idea would be that Restfulie isn't initialized correctly. I put a require in the spec_helper and didn't help either.

abstractj commented 13 years ago

hi hemju, did you already try to look at https://github.com/caelum/restfulie-restbuy ?

ghost commented 13 years ago

Hi,

yes I already looked at the example. However, the example isn't really useful because it just shows how to test with the Restfulie client https://github.com/caelum/restfulie-restbuy/blob/master/spec/client_spec.rb . I want to test the raw response without any dependencies to restfulie. I want to parse the JSON/XML and check if it what I expected.

Nevertheless, nothing of this explains why tokamak isn't use to build the response. I am really stuck on this one, so any help would be appreciated.

guilhermesilveira commented 13 years ago

looking into it now

guilhermesilveira commented 13 years ago

Hi everyone,

I've tested with the following versions: rspec, rspec-rails 2.5.0 rails 3.0.7 restfulie 1.0.0 tokamak 1.1.2

Everything worked as expected (the body is there). Can you check the following git project? Notice that I have commited the gemfile.lock so it uses the exact same gem versions.

https://github.com/guilhermesilveira/restfulie-rspec-helmut

Regards

hemju commented 13 years ago

Yes, your project works, but there is a difference. You don't use tokamak files. Add index.tokamak to your clients view and nothing is returned.

hemju commented 13 years ago

Any progress on this issue?

guilhermesilveira commented 13 years ago

looking at it. didnt find the cause yet.

guilhermesilveira commented 13 years ago

the problem is somewhere within the compile method:

    def compile(template)
      "@content_type_helpers = @controller.hypertemplate_registry[self.response.content_type].helper; " +
      "extend @content_type_helpers; " +
      "extend Hypertemplate::Hook::Rails::Helpers; " +
      "code_block = lambda { #{template.source} };" +
      "builder = code_block.call; " +
      "builder"
    end

Builder is empty as code_block is nul for some weird reason. This means that the template invocation is returnin null instead of returning its content (the build or build_dsl method). Can you try helping me detecting the problem with build or build_dsl?

guilhermesilveira commented 13 years ago

btw the problem is from rails 3.0.0 to rails 3.0.7 so it has been around all the time

hemju commented 13 years ago

Hey, I just debugged compile and I came to action_controller/test_case.rb where the result ActiveSupport::Notifications.subscribe("!render_template.action_view") was used. So this is a setting that the view intentionally isn't rendered. So I started googling and came across http://www.simonecarletti.com/blog/2011/04/rspec-rails-doesnt-render-rails-views-by-default/ which finally let me to the offical RSpec docu where it says that the view isn't rendered by default in controller specs, but you can turn it on with render_views

So this hasn't to do anything about tokamak, it is rspec. However, I would consider to add a note in the tokamak read me.

guilhermesilveira commented 13 years ago

Couldnt be more thankful. Can you close the issue then?

I've added to the homepage: https://github.com/caelum/hypertemplate

Unfortunately we forked tokamak to be able to commit to the changes: https://github.com/abril/tokamak/pull/1

Regards

hemju commented 13 years ago

Yes, close the issue. Does this mean that restfulie uses hypertemplate and not tokamak?

guilhermesilveira commented 13 years ago

Yep. We forked it to apply the missing patches (both dsls and others).