views:

13

answers:

1

I'm attempting the following functional test on the controller. I'm using the following

  • RSpec2
  • Rails 3
  • Shoudla
  • Mocha

Here's the test

context "POST on create should be successful" do
  before(:each) do
    Model.any_instance.expects(:save).returns(true).once
    Model.any_instance.stubs(:id).returns(1)
    post :create, :model => {}
  end

  it { should assign_to(:model).with_kind_of(Model) }
  it { should set_the_flash.to("Model was created successfully.")}
  it { should redirect_to(model_path(1))}
end

and the controller under test is

def create
  @model = Model.new(params[:model])

  if @model.save
    flash[:success] = "Model was created successfully."
  end
  respond_with @model
end

The only failing test is the third test which says that the path getting returned is "http://test.host/models" instead of "http://test.host/models/1"

When I'm running the application in the browser I am getting the correct redirection.

A: 

I think you need mock to_params too. It's use by route system

context "POST on create should be successful" do
  before(:each) do
    Model.any_instance.expects(:save).returns(true).once
    Model.any_instance.stubs(:id).returns(1)
    Model.any_instance.stubs(:to_params).returns(1)
    post :create, :model => {}
  end

  it { should assign_to(:model).with_kind_of(Model) }
  it { should set_the_flash.to("Model was created successfully.")}
  it { should redirect_to(model_path(1))}
end
shingara
I'm still getting the same result with the additional stub. I tried to override both 'to_param' and 'to_params'. I think you meant 'to_param' for the override.
Steve Mitcham