如何在视图中为更新操作编写rspec?

时间:2015-05-01 08:24:03

标签: ruby-on-rails rspec

只有属性策展人等于true的用户才能将文章中的主页列表更新为true,并且工作正常。

<% if @article.homepagelist%>
    <% if current_user.curator %>
        <%= form_for @article do|f| %>
            <%= f.hidden_field :article_id, :value => @article.id%>
            <%= f.hidden_field :title, :value => @article.title%>
            <%= f.hidden_field :user_id, :value => current_user.id %>
            <%= f.hidden_field :body, :value => @article.body%>
            <%= f.hidden_field :image, :value => @article.image%>
            <%= f.hidden_field :plain_body, :value => @article.plain_body %>
            <%= f.hidden_field :magazine_id, :value => @article.magazine_id%>
            <%= f.hidden_field :is_sponsored, :value => @article.is_sponsored%>
            <%= f.hidden_field :ad_title, :value => @article.ad_title %>
            <%= f.hidden_field :homepagelist, :value => false %>
            <%= f.submit "Delete from List" %>
        <% end %>
    <% end %>
    <% else %>
    <div class="col-md-2"></div>
        <% if current_user.curator %>
        <%= form_for @article do|f| %>
            <%= f.hidden_field :article_id, :value => @article.id%>
            <%= f.hidden_field :title, :value => @article.title%>
            <%= f.hidden_field :user_id, :value => current_user.id %>
            <%= f.hidden_field :body, :value => @article.body%>
            <%= f.hidden_field :image, :value => @article.image%>
            <%= f.hidden_field :plain_body, :value => @article.plain_body %>
            <%= f.hidden_field :magazine_id, :value => @article.magazine_id%>
            <%= f.hidden_field :is_sponsored, :value => @article.is_sponsored%>
            <%= f.hidden_field :ad_title, :value => @article.ad_title %>
            <%= f.hidden_field :homepagelist, :value => true %>
            <%= f.submit "Add List" %>
        <% end %>
    <% end %>
<% end %>

我想在点击添加到列表后删除更新操作,或者从列表中删除仅当我点击添加到列表时将文章中的主页列表变量更改为true,当我点击列表中的rmove时将其更改为false

describe 'POST #update/homepagelistvariable' do
    it 'allows curator to update homepagelist' do
      @user = create(:user)
      sign_in @user
      @user.curator = true
      @article2 = create(:article)
      @article2.homepagelist = true
      patch :update, article: FactoryGirl.attributes_for(:article,
                                                        homepagelist:
                                                        @article2.homepagelist)

    end
  end

1 个答案:

答案 0 :(得分:0)

  describe 'PUT #update/homepagelist' do
    it 'curator can add article to homepagelist' do
      @user = create(:user, :curator => true)
      sign_in @user
      @article = create(:article, :user_id  => @user.id)
      put :update, id: @article.id,
                   article: FactoryGirl.attributes_for(:article,
                                                       homepagelist:
                                                      'true')
      @article.reload
      expect(assigns(:article).homepagelist).to match(true)
    end
  end
  it 'curator can remove article to homepagelist' do
    @user = create(:user, :curator => true)
    sign_in @user
    @article = create(:article, :user_id  => @user.id,
                     :homepagelist => true )
    put :update, id: @article.id,
                 article: FactoryGirl.attributes_for(:article,
                                                     homepagelist:
                                                    'false')
    @article.reload
    expect(assigns(:article).homepagelist).to match(false)
  end
end