Testing "Create a Post" with Rspec

I am trying to test the "Create Record" action using Rspec. The code is as follows:

def valid_attributes { :zone => Flymgr::Zone.new(:countries => Flymgr::ZoneCountry.first, :name => 'USA', :description => 'USA Flight', :zipcodes => ''), :price => '100.00', :class => 'first', } end def valid_session {} end before(:each) do @request.env["devise.mapping"] = Devise.mappings[:admin] admin = FactoryGirl.create(:admin) sign_in admin end describe "POST create" do describe "with valid params" do it "creates a new Flymgr::Rule" do expect { post :create, {:Flymgr_rule => valid_attributes} }.to change(Flymgr::Rule, :count).by(1) end 

One of the necessary attributes for the form is the "zone", this is a drop-down list, and the parameters for the drop-down list are created with another form. I do not know how to create a form entry using rspec. As you can see, I tried to call a method from another Flymgr::Zone.new . I do not think this works, and it breaks my test.

Can anyone advise a better way to do this? Maybe I should use FactoryGirl to create a zone and rule record?

+6
source share
2 answers

your request parameter hash has an object as the value: zone, when you publish it, it will just be "to_s'-ed", which is unlikely what you want.

In general, it is best to use a factory girl to create your objects and use attributes for a strategy to parameterize your attributes for a mail request: What is the correct way to test the controller’s “create” actions?

Your question assumes the association is an affiliation, so you just need to place the identifier. Keep in mind that FactoryGirl does not currently create any attributes for associations. If your factory definition for a rule takes care of zone association, you can use this workaround:

 FactoryGirl.build(:flymgr_rule).attributes 

also include zone_id, but then you need to exclude unwanted parameters. ("id", "created_at", "updated_at", etc.).

Thus, you might be better off explicitly inserting the params hash information for the zone as you see it in the actual mail request.

Read this thread about factorygirl attributes and associations: https://github.com/thoughtbot/factory_girl/issues/359

+5
source

As shown in the manual :

 # Returns a hash of attributes that can be used to build a User instance attrs = FactoryGirl.attributes_for(:user) 
+3
source

Source: https://habr.com/ru/post/915493/


All Articles