RSpec - 相同的字符串比较失败
RSpec - Identical strings failing comparison
我正在 运行测试我在 Rails 中编写的 XML 解析器的输出。当我 运行 RSpec 它失败并向我显示它 'expected' 和它 'got' 的内容并且它们的内容至少是相同的。
我试过使用 eq、eql、equal 和 match,但 none 有效。
测试:
expect(sql).to eql("INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n")
创建输出的解析器部分:
@sql << "INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (" +
"#{self.last_user[:id]}, " +
"'#{clean(self.last_user[:first_name])}', " +
"'#{clean(self.last_user[:last_name])}', " +
"'#{clean(self.last_user[:city])}', " +
"'#{clean(self.last_user[:street_address])}', " +
"'#{clean(self.last_user[:country])}', " +
"'#{clean(self.last_user[:postcode])}', " +
"'#{clean(self.last_user[:company])}', " +
"'#{clean(self.last_user[:email])}', " +
"'#{clean(self.last_user[:username])}', " +
"'#{clean(self.last_user[:phone_number])}', " +
"CURRENT_TIMESTAMP, " +
"CURRENT_TIMESTAMP" +
");\n"
失败的测试:
1) UserImporter with real data creates an SQL file ready to import the article
Failure/Error: expect(sql).to eql("INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n")
expected: "INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, emai...C', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n"
got: "INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, emai...C', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n"
(compared using eql?)
Diff:
@@ -1,2 +1,3 @@
INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
+INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
问题是我的代码将该行打印了两次。多亏了 7stud,我发现了这个错误:
You see the two INSERT lines after the @@...@@? The first one starts with a space(i.e. not a - or a +). That means the line is common to both files. The next line starts with a +. That means the new file has that additional line, i.e. the new file has two INSERT lines while the old file has one INSERT line. And, as stated before the new file is the actual, and the old file is the expected. (If a line starts with a -, then the old file has that line, but the new file does not.)
我正在 运行测试我在 Rails 中编写的 XML 解析器的输出。当我 运行 RSpec 它失败并向我显示它 'expected' 和它 'got' 的内容并且它们的内容至少是相同的。
我试过使用 eq、eql、equal 和 match,但 none 有效。
测试:
expect(sql).to eql("INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n")
创建输出的解析器部分:
@sql << "INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (" +
"#{self.last_user[:id]}, " +
"'#{clean(self.last_user[:first_name])}', " +
"'#{clean(self.last_user[:last_name])}', " +
"'#{clean(self.last_user[:city])}', " +
"'#{clean(self.last_user[:street_address])}', " +
"'#{clean(self.last_user[:country])}', " +
"'#{clean(self.last_user[:postcode])}', " +
"'#{clean(self.last_user[:company])}', " +
"'#{clean(self.last_user[:email])}', " +
"'#{clean(self.last_user[:username])}', " +
"'#{clean(self.last_user[:phone_number])}', " +
"CURRENT_TIMESTAMP, " +
"CURRENT_TIMESTAMP" +
");\n"
失败的测试:
1) UserImporter with real data creates an SQL file ready to import the article
Failure/Error: expect(sql).to eql("INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n")
expected: "INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, emai...C', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n"
got: "INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, emai...C', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n"
(compared using eql?)
Diff:
@@ -1,2 +1,3 @@
INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
+INSERT INTO users (id, first_name, last_name, city, street_address, country, postcode, company, email, username, phone_number, created_at, updated_at) VALUES (1, 'Dewayne', 'Dare', 'Rippinfort', '1171 Lucas Place', 'Malaysia', '94876-5561', 'Kiehn LLC', 'turner@schmidt.biz', 'kay_armstrong', '652.488.7428', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
问题是我的代码将该行打印了两次。多亏了 7stud,我发现了这个错误:
You see the two INSERT lines after the @@...@@? The first one starts with a space(i.e. not a - or a +). That means the line is common to both files. The next line starts with a +. That means the new file has that additional line, i.e. the new file has two INSERT lines while the old file has one INSERT line. And, as stated before the new file is the actual, and the old file is the expected. (If a line starts with a -, then the old file has that line, but the new file does not.)