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

私は自分で解決策を見つけました。アルバム ファクトリでは、 と が確実に一致するように、さらに属性を追加する必要が@userありalbum.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 に答える