Gaz*_*ler 10 rspec ruby-on-rails-3
我的测试数据库出现问题,每次运行后都没有擦除数据.我也有黄瓜测试,每次运行时都会清除数据库.
以下规范测试仅在rake db:test:prepare之后立即生效,我的测试或spec_helper.rb是否有问题导致数据持续存在?
我的规格测试是:
require "spec_helper"
describe "/api/v1/offers", :type => :api do
Factory(:offer)
context "index" do
let(:url) { "/api/v1/offers" }
it "JSON" do
get "#{url}.json"
last_response.status.should eql(200)
last_response.body.should eql(Offer.all.to_json(:methods => [:merchant_image_url, :remaining_time, :formatted_price]))
projects = JSON.parse(last_response.body)
projects.any? { |p| p["offer"]["offer"] == "Offer 1" }.should be_true
end
it "XML" do
get "#{url}.xml"
last_response.body.should eql(Offer.all.to_xml(:methods => [:merchant_image_url, :remaining_time, :formatted_price]))
projects = Nokogiri::XML(last_response.body)
projects.css("offer offer").text.should eql("Offer 1")
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的spec/spec_helper.rb文件如下所示:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
Run Code Online (Sandbox Code Playgroud)
干杯,Gazler.
zet*_*tic 16
工厂需要进入一个before(:each)区块:
describe "/api/v1/offers", :type => :api do
before(:each) do
Factory(:offer)
end
context "index" do
... etc ...
Run Code Online (Sandbox Code Playgroud)
before(:each)运行每个示例后,RSpec将回滚块中创建的所有行.
| 归档时间: |
|
| 查看次数: |
5882 次 |
| 最近记录: |