2014-01-27 5 views
0

Есть ли способ, чтобы реорганизовать это, чтобы избежать повторения:Как я могу Refactor эти утверждения, чтобы избежать дублирования

post :create, user: attributes_for(:user) 

Учитывая, что первое утверждение нужно обернуть его в expect блоке, я не см. способ перемещения его в блок before. Очевидно, я мог бы обернуть последние два утверждения в блоке context или describe со своим собственным блоком before, но это не так.

context 'with valid attributes' do 

    it 'should create a new User and save it to the database' do 
     expect { 
     post :create, user: attributes_for(:user) 
     }.to change(User, :count).by(1) 
    end 

    it { 
     post :create, user: attributes_for(:user) 
     should redirect_to(user_path(assigns[:user])) 
    } 

    it { 
     post :create, user: attributes_for(:user) 
     should set_the_flash[:notice] 
    } 

    end 

ответ

1

Вы можете поставить "действие" в методе, следующим образом:

context 'with valid attributes' do 
    it 'should create a new User and save it to the database' do 
    expect { 
     do_action 
    }.to change(User, :count).by(1) 
    end 

    it { 
    do_action 
    should redirect_to(user_path(assigns[:user])) 
    } 

    it { 
    do_action 
    should set_the_flash[:notice] 
    } 

    def do_action 
    post :create, user: attributes_for(:user) 
    end 
end 
0

что-то вроде этого (не убедитесь, что он работает)

context 'with valid attributes' do 
    before { post :create, user: attributes_for(:user) } 

    it 'should create a new User and save it to the database' do 
     expect(User.count).to eq 1 
    end 

    it { should redirect_to(user_path(assigns[:user])) } 
    it { should set_the_flash[:notice] } 
    end 

 Смежные вопросы

  • Нет связанных вопросов^_^