CanCeylan
CanCeylan

Reputation: 3010

Passing Object's Params into Rspec test with params.merge

In my rspec test, I create an object with factorygirl, and in my create controller, I need parameters in order to create an object. Since there are lots of parameters and connection with paperclip, I do not want to give them by hand. My code is the following:

  it "should render correct JSON on success save" do
    @params = {}
    post(:create, @params.merge(:upload => FactoryGirl.create(:upload)))
    response.body.should == @upload.to_json
  end

So my problem is initializing the :upload's params. I'm already creating @upload object with factorygirl at the beginning with before

How should I solve this problem efficiently ?

Thanks.

Upvotes: 0

Views: 1795

Answers (1)

ian
ian

Reputation: 12251

The post is not part of the spec, it is a change of state that occurs before the response so it should go in a before block.

The params don't need to be an instance variable (does anything in RSpec?), better to use a let.

context "When saving" do

 before do
   post(:create, params)
 end

 subject { response.body }

 context "That is successful" do
   let(:upload) { FactoryGirl.attributes_for(:upload) }
   let(:params){
     {:upload => upload }
   }
   it { should == upload.to_json }
 end

 context "That is not successful" do
   # do some failure stuff here
 end
end

Upvotes: 1

Related Questions