我有一个失败的Jasmine测试,因为正在生成一个随机数,这个随机值对于执行和规范是不同的。
fetch: function(options) {
if(typeof options === "undefined") {
options = {};
}
if(typeof options.data === "undefined") {
options.data = {};
}
options.data.id = this.journalId;
options.data.random = Math.floor(Math.random()*10000);
col.prototype.fetch.call(this, options);
}
下面的测试失败,因为Math.floor(Math.random()*10000)
生成了不同的值。
it("should call parent fetch with default options", function() {
this.collection.fetch();
expect(this.fetchSpy).toHaveBeenCalledWith({
data: {
id: 1,
random: Math.floor(Math.random()*10000)
}
});
});
有没有办法让我的测试通过我生成随机数的情况?
答案 0 :(得分:7)
您可以模拟Math.random
功能。茉莉花2:
it("should call parent fetch with default options", function() {
spyOn(Math, 'random').and.returnValue(0.1);
this.collection.fetch();
expect(this.fetchSpy).toHaveBeenCalledWith({
data: {
id: 1,
random: Math.floor(Math.random()*10000)
}
});
});
Jasmine 1:
it("should call parent fetch with default options", function() {
jasmine.spyOn(Math, 'random').andReturn(0.1);
this.collection.fetch();
expect(this.fetchSpy).toHaveBeenCalledWith({
data: {
id: 1,
random: Math.floor(Math.random()*10000)
}
});
});
答案 1 :(得分:1)
你不应该spyOn import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class PizzaSystem {
private JFrame frame;
// Toppings toppingsPanel;
MainMenu menuPanel;
JButton loginBtn;
JPanel mainPanel;
CardLayout cl;
//MAIN
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PizzaSystem window = new PizzaSystem();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//Constructor
public PizzaSystem() {
initialize();
}
//Initialize the GUI
private void initialize() {
cl = new CardLayout();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel(cl);
frame.getContentPane().add(mainPanel);
JPanel panel_1 = new JPanel(new GridBagLayout());
loginBtn = new JButton();
loginBtn.setText("Login");
loginBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showCard("menu");
}
});
panel_1.add(loginBtn);
mainPanel.add(panel_1, "loginPanel");
JPanel menuPanel = new MainMenu();
mainPanel.add(menuPanel, "menu");
frame.pack();
showCard("loginPanel");
}
//Call card matching the key
public void showCard(String key) {
cl.show(mainPanel, key);
}
public class MainMenu extends JPanel {
public MainMenu() {
JPanel panel1 = new JPanel(new BorderLayout());
JLabel logoLabel = new JLabel("Logo");
panel1.add(logoLabel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setToolTipText("");
panel1.add(tabbedPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
tabbedPane.addTab("MENU", null, panel, null);
//*******************************************************************************************************
//Pepperoni menu section
JLabel pizza1Image = new JLabel("IMAGE");
panel.add(pizza1Image);
JLabel pepperoniLabel = new JLabel("PEPPERONI");
pepperoniLabel.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(pepperoniLabel);
JButton pepperoniOrderBtn = new JButton("ORDER");
pepperoniOrderBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//Pepperoni End
JButton accountButton = new JButton("Account");
panel.add(accountButton);
JButton checkoutButton = new JButton("Checkout");
panel.add(checkoutButton);
JButton logoutButton = new JButton("Logout");
panel.add(logoutButton);
JTextArea txtrOrderInfoGoes = new JTextArea(10, 20);
txtrOrderInfoGoes.setText("Order Info\r\nGoes Here\r\n\r\nPepperoni Pizza x1\r\n$6.50");
panel.add(txtrOrderInfoGoes);
JButton clearOrderButton = new JButton("Clear");
panel.add(clearOrderButton);
JLabel titleLabel = new JLabel("MAMA JANE'S PIZZERIA");
panel.add(titleLabel);
titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.PLAIN, 50));
JLabel storeInfoLabel = new JLabel("<html>1234 Fakelane Rd <br> 10001, Faketopia, USA <br> 111-11-PIZZA <br> Mon-Fri 11AM to 10pm <br> Sat-Sun 10AM to 11pm");
panel.add(storeInfoLabel);
JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane);
logoutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
add(panel1);
}
}
}
。
有很多包使用这个功能(甚至Jasmine),更好的方法是编写一个方法来包装随机的东西。
Math.random
现在你可以模拟它了!
class Fetcher {
fetch(url) {
const rand = this.generateRandomNumber(64);
// ...
}
generateRandomNumber(n) {
return Math.floor(Math.random() * n);
}
}
答案 2 :(得分:0)
如果你想模拟一系列&#34;随机&#34;值,您可以使用:
spyOn(Math, 'random').and.returnValues(0.1, 0.2, 0.3, 0.4);
请注意,您必须在returnValue s 中添加 s ,否则它将无法正常工作