Topic.count没有改变1

时间:2017-11-07 20:04:34

标签: ruby-on-rails ruby testing rake

我正在测试我的主题控制器的create方法并继续收到此错误:

"Topic.count" didn't change by 1.
Expected: 4
  Actual: 3

我尝试了一系列不同的解决方案,从更改我在params中发送的属性数量到改变属性本身,但似乎没有任何工作。有什么想法吗?

这是我的创建方法:

def create
    topic = current_user.topics.create(topic_params)
    if topic.save
      redirect_to topic
      unless current_user.reload.spammer?
        AdminMailer.topic_created(topic).deliver_now
      end
    else
      redirect_to topics_path, flash: 'Failed to save this question. Please try again or contact email@domain.com'
    end
  end

这是我的测试:

test "should create topic" do
    assert_difference 'Topic.count' do 
        post topics_url, params: {topic: {title: 'this is a topic', message_attributes: {text: 'this is the question'}, user_id: @user.id, bound_id: @bound.id}}
    end
    assert_redirected_to topics_url(Topic.last)
  end

0 个答案:

没有答案