0

所以我正在尝试使用 POST 方法创建一个项目。但它似乎没有发布该项目。当我得到/api/albums似乎反应良好并且我的测试通过时。

/album_spec.rb

describe AlbumController do
  before :each do
    @user =FactoryGirl.create(:person)
    @album = FactoryGirl.create(:album)
  end

  it "creates first story on login", :js => true do
    login(@user)
    post "/api/albums/", :format => :api_v1,:album =>@album
    response.should be_success

  end
  it "gets album", :js=> true do
    get "api/albums/"
    response.should be_success
  end
end

spec_helper.rb

require 'rubygems'
require 'spork'

#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'
  require "capybara-screenshot"
  require "factory_girl_rails"

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

  #Capybara.defualt_wait_time = 10

  if ENV['BROWSER_TEST']
    Capybara.register_driver :selenium do |app|
      Capybara::Selenium::Driver.new(app, :browser => :chrome)
    end
  else
    Capybara.javascript_driver = :selenium
  end

  RSpec.configure do |config|
    config.include Capybara::DSL
    config.include(EmailSpec::Helpers)
    config.include(EmailSpec::Matchers)
    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true
    config.include RequestHelpers, :type => :request

    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

    config.infer_base_class_for_anonymous_controllers = false
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
  FactoryGirl.reload
end
4

1 回答 1

1

我为自己找到了解决方案。在专辑工厂中,我不得不添加更多属性以确保@useralbum.user匹配。

@album = FactoryGirl.create(:album,person_id=>@user.id)

然后我的测试用例看起来像这样

it "creates first story on login"do

login_as @user
post "/api/albums/", :format =>":api_v1",:album =>@album.attributes
response.should be_success
a = Album.last  
a.person_id.should == @user.id #verifying user_ids match end
end
于 2014-06-04T14:12:31.763 回答